关于限制物体旋转角度的问题

第一种:这个比较笨,但是也能实现

建立一个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);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容