第一種:這個(gè)比較笨肴盏,但是也能實(shí)現(xiàn)
建立一個(gè)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);
}
}
第二種:通過鼠標(biāo)控制物體的旋轉(zhuǎn)角度帽衙,
public Transform rotateTarget;? ? ? //? 限制旋轉(zhuǎn)對象
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);
}