剩下兩週上課囉,大家加油!!
在一開始我們在課程完成了椅子(角色的移動嘛),但遠遠的看著他移動好像缺少了一些遊戲體驗 嗎?><
這週我們就來改變遊戲的視角吧~~

來看一下兩個版本的比較~~
原本WASD AD控制Y軸旋轉(轉頭),後來改成WASD控制平移座標,把旋轉特別移出來到滑鼠上,滑鼠可以控制Y軸及另一軸的旋轉(轉頭、仰頭低頭)
V1.0版本
public class ChairMove : MonoBehaviour
{
public float Speed = 1.0f;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
this.gameObject.transform.Translate(Input.GetAxis("Vertical") * Speed * Time.deltaTime, 0, 0);
this.gameObject.transform.Rotate(0, Input.GetAxis("Horizontal") * 60 * Time.deltaTime, 0);
BaseMove();
}
private void BaseMove()
{
if (Input.GetKeyDown(KeyCode.Space))
//if (Input.GetKeyDown("Space")) 或打這句話也行
{
rb.AddForce(new Vector2(0, 200));
}
}
}
V2.0版本
public class ChairCamera : MonoBehaviour
{
public float speed = 1.0f;
//滑鼠
private Vector2 rotation; //旋轉值
public Vector2 sensitivity; //滑鼠靈敏度
private Vector2 GetmouseInput()
{
Vector2 input = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
return input;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Camera1Move();
if (Input.GetKey(KeyCode.Mouse2))
{
MouseControl();
}
}
private void Camera1Move()
{
this.gameObject.transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime );
}
private void MouseControl()
{
rotation += GetmouseInput() * sensitivity * Time.deltaTime; //滑鼠移動
this.gameObject.transform.localEulerAngles = new Vector3(-rotation.y, rotation.x, 0); //滑鼠鏡頭角度
}
}
接著我們就細講囉~~
一樣先準備............一張椅子(角色)、一個不會掉下去的地板
#椅子讓他乾淨,不綁腳本(Script)在身上

剛剛有說把滑鼠變成移動視角嘛,換句話說就是椅子就要跟相機一起。
所以就是把相機(視角)椅子(物件)用空物件(GameObject)綁一起。


像是這樣!!(這時候要確認一下,空物件跟相機的XYZ座標要對得上哦)
好~等等會把平移+視角的移動寫在同一個腳本,並且賦予在空物件上
新增一個腳本 叫ChairCameraMove
,先把他加在Character(空物件)上
打開腳本後,我們先處理平移部分

打個速度的公開變數,等等才可去外面方便調整
public float speed = 1.0f;

之後寫一個移動的方法
private void
CharacterMove
()
{
this.gameObject.transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
}

CharacterMove
();
移動跟之前一樣,讓他每分每秒都要持續偵測,所以要寫在Update 方法內讓他跑。
不過跟之前比較不一樣。之前是直接打在Update裡面嘛,要另外寫個方法是因為,後續如果有B、C、D等等角色也想要這方法,直接呼叫方法名稱就可以用了。

這前一個腳本打的 :)
(先試看看可不可以正常移動)
OK就繼續下一步,滑鼠移動~~

宣告三個變數 ROTATION、SENSITIVITY、GAMEMOUSEINPUT
private Vector2 rotation; //旋轉值
public Vector2 sensitivity; //滑鼠靈敏度
private Vector2 GetmouseInput()
{
Vector2 input = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
return input;
}
#
- 使用
Input.GetAxis("Mouse X")
和Input.GetAxis("Mouse Y")
來獲取滑鼠在水平方向和垂直方向上的移動。 - 這些輸入值被儲存在一個
Vector2
向量中並返回。

然後寫一個方法滑鼠移動~~
private void
MouseControl()
{
rotation += GetmouseInput() * sensitivity * Time.deltaTime; //滑鼠移動
this.gameObject.transform.localEulerAngles = new Vector3(-rotation.y, rotation.x, 0); //滑鼠鏡頭角度
}
- 首先,用 GetmouseInput 方法來獲取滑鼠輸入。
- 將得到的滑鼠輸入乘以 sensitivity,來控制旋轉的靈敏度。
- 乘以 Time.deltaTime,以使旋轉角度與每幀之間的時間間隔成比例,保持旋轉的平滑性。
- 最後,將計算出的旋轉增量加到 rotation 上,更新當前的旋轉值。

- 將更新後的旋轉值應用到當前遊戲物體的原地旋轉角度。
rotation.x
用來控制水平方向的旋轉(左右)。rotation.y
用來控制垂直方向的旋轉(上下),並且取反(-rotation.y
),這樣滑鼠向上移動時視角會向上移動,向下移動時視角會向下移動,符合一般用戶的操作習慣。
2D 3D邏輯有點不一樣 :)
#這個如果不懂正負,直接遊戲試試比較快~

疑,等等。 +=?
+=這什麼,他是指定運算子,舉例最快~~
i=5; ....這個很直覺就是i等於5,沒錯吧
i+=5;...那這個的話大家就比較不知道了,因為我們平常不會這樣算,他就是 i=i+5,會一直壘加上去
那如果我把剛剛 rotation 的+拿掉 ,就會變成,每次動都會歸零。不適用於轉視角這~
再把Rigidbody的Rotation凍結起來~,不然撞到東西會東倒西歪

最後成果展示,變得更讚了~
