遊戲素材
開始製作遊戲
新建一個 2D 專案
將開頭的兩個素材拉進專案的資源管理器
將 roulette (輪盤) 拉進階層視窗
同樣的將 needle (指針) 拉進階層視窗,並設定屬性位置為 0,3,0,圖層順序1
選到階層視窗的 Main Camera ,將背景顏色修改為 FBFBF2 (淺黃色)

完成
撰寫 C# 腳本
新增一個 C# 腳本,名為「RouletteController」
將以下程式碼新增至腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RouletteController : MonoBehaviour
{
// Start is called before the first frame update
float rotSpeed = 0;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
rotSpeed = Random.Range(10.0f, 30.0f);
transform.Rotate(0, 0, rotSpeed);
rotSpeed *= 0.995f;
}
}
將 RouletteController 腳本新增到 roulette 物件底下
完成,看看結果吧

讓程式知道你現在停在哪裡
加入一個文字的 UI 在階層視窗,設定 Canvas 的渲染模式為螢幕空間 – 攝影機
渲染攝影機設為 MainCamera
設定一下文字的屬性讓他可以在螢幕上被看到
完成

獲取面板中的角度值與顯示運勢文字
修改剛剛的 RouletteController.cs 腳本為以下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RouletteController : MonoBehaviour
{
// Start is called before the first frame update
float rotSpeed = 0;
private Text Angle;
void Start()
{
Angle = GameObject.Find("Text").transform.GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
rotSpeed = Random.Range(10.0f, 30.0f);
transform.Rotate(0, 0, rotSpeed);
rotSpeed *= 0.995f;
float now_rotation = transform.localEulerAngles.z;
string now_status = string.Empty;
if (now_rotation > 0 && now_rotation <= 30)
now_status = "凶";
else if (now_rotation > 30 && now_rotation <= 90)
now_status = "大吉";
else if (now_rotation > 90 && now_rotation <= 150)
now_status = "大凶";
else if (now_rotation > 150 && now_rotation <= 210)
now_status = "小吉";
else if (now_rotation > 210 && now_rotation <= 270)
now_status = "末吉";
else if (now_rotation > 270 && now_rotation <= 330)
now_status = "中吉";
else if (now_rotation > 330)
now_status = "凶";
Angle.text = "運勢:" + now_status + "\n角度:" + now_rotation.ToString();
}
}
完成

Latest posts by SHXJ (see all)
- 受保護的內容: NAS 版 Mathbot 管理網站與 Linebot 啟動方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 數學遊戲 - 2021 年 6 月 8 日








在〈占卜輪盤〉中有 1 則留言