腳本要掛載在相機上
移動,實現左右和上下移動
void Update()
{
float h = Input.GetAxis("Horizontal"); //檢測水平方向鍵
float v = Input.GetAxis("Vertical"); //檢測垂直方向鍵
transform.Translate(new Vector3(h, v, 0)*speed); //左右上下移動,可以修改爲前後左右移動
}
旋轉
void Update()
{
//檢測水平和垂直方向鍵
float h = Input.GetAxis("Horizontal") * speed;
float v = Input.GetAxis("Vertical") * speed;
// 獲得鼠標當前位置的X和Y
//float h = Input.GetAxis("Mouse X") * speed;
//float v = Input.GetAxis("Mouse Y") * speed;
// 鼠標在Y軸上的移動號轉爲攝像機的上下運動,即是繞着X軸反向旋轉
transform.localRotation = transform.localRotation * Quaternion.Euler(-v, 0, 0);
// 鼠標在X軸上的移動轉爲主角左右的移動,同時帶動其子物體攝像機的左右移動
transform.localRotation = transform.localRotation * Quaternion.Euler(0, h, 0);
}