Unity で 3D ゲームを作成しており、プレイヤーがマウスで見回すスクリプトがあります。プレイヤーが見ている方向に移動するために、transform.forward を使用しています。私の問題は、彼らが天井を見て 'W' (前方) を押すと、空中に上昇し始めることです。transform.forward
基本的に、x 軸と z 軸でのみ移動できるメソッドまたはサブメソッドがあるかどうかを知る必要があります。
ここに私の動きのスクリプト(C#)があります:
if (transform.rotation.x < -10)
{
//do no forward or backward movement
Debug.Log("Rotation too great to forward move...");
tooGoodForMovement = true;
}
else
{
tooGoodForMovement = false;
if (Input.GetKey(KeyCode.W))
{
//Forward
player.velocity = (transform.FindChild("Main Camera").transform.forward * moveSpeed);
}
if (Input.GetKey(KeyCode.S))
{
//Back
player.velocity = (-transform.FindChild("Main Camera").transform.forward * moveSpeed);
}
}
if (Input.GetKey(KeyCode.A))
{
//Left
player.velocity = -transform.FindChild("Main Camera").transform.right * moveSpeed;
}
if (Input.GetKey(KeyCode.D))
{
//Right
player.velocity = transform.FindChild("Main Camera").transform.right * moveSpeed;
}