美文网首页
关于限制物体旋转角度的问题

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

作者: 吐泡泡的小鲤鱼 | 来源:发表于2017-05-17 21:05 被阅读0次

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

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

}

相关文章

网友评论

      本文标题:关于限制物体旋转角度的问题

      本文链接:https://www.haomeiwen.com/subject/xxkjxxtx.html