你查看过 Unity 提供的太空射击教程吗?
https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial/moving-player?playlist=17147
它是在旧版本的 Unity(Unity 版本 4)中编码的,但他们会检查以确保它与新版本兼容(截至今天的 Unity 版本 5.1)。如果链接更改,我已经复制了他们在视频中使用的代码。
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
这rigidBody.rotation
将用于您提供的图片中所示的倾斜/坡度,但目前不会改变船的正向旋转。按左右箭头键会影响刚体速度并导致倾斜,一旦松开将恢复到中心船位置。