第一种:这个比较笨,但是也能实现
建立一个Cube,将该脚本挂到Cube上就可以了
int RotateSpeed = 15;
void Update () {
if (Input.GetKey(KeyCode.A) && (this.transform.localEulerAngles.y <= 90 || this.transform.localEulerAngles.y >= 270))
{
this.transform.Rotate(Vector3.down * Time.deltaTime * RotateSpeed);
}
else if (Input.GetKey(KeyCode.D) && (this.transform.localEulerAngles.y <= 89 || this.transform.localEulerAngles.y >= 269))
{
this.transform.Rotate(Vector3.up * Time.deltaTime * RotateSpeed);
}
}
第二种:通过鼠标控制物体的旋转角度,
public Transform rotateTarget; // 限制旋转对象
float moveSpeed = 10;
float minAngleY = 80;
float maxAngleY = 110;
float minAngleX = -20;
float maxAngleX = 20;
float rotationY = 0;
float rotationX = 0;
void Update()
{
if (null == rotateTarget)
return;
rotationX += Input.GetAxis("Mouse X") * moveSpeed;
rotationX = Mathf.Clamp(rotationX, minAngleX, maxAngleX);
rotationY += Input.GetAxis("Mouse Y") * moveSpeed;
rotationY = Mathf.Clamp(rotationY, minAngleY, maxAngleY);
rotateTarget.localEulerAngles = new Vector3(-rotationY, rotationX, rotateTarget.rotation.z);
}