如何在使用变换时限制对象的旋转.旋转()



我试图限制对象在x轴上的旋转。旋转执行如下:

float moveVertical = Input.GetAxis("Vertical");
transform.Rotate(moveVertical * rotationRate * Time.deltaTime, 0, 0);

我试着这样做:

if( transform.rotation.x > -89f && tranform.rotation.x < 89f) 
{
transform.Rotate(moveVertical * rotationRate * Time.deltaTime, 0, 0); 
}

但随后Unity中断,PlayMode根本没有打开。。。我必须通过任务管理器重新启动程序。

似乎你的Unity没有播放的原因是你的代码中有一个错误,并且"变换";在你的if语句中拼写错误,所以这应该解决这个问题:

if(transform.rotation.x > -89f && transform.rotation.x < 89f) 
{
transform.Rotate(moveVertical * rotationRate * Time.deltaTime, 0, 0); 
}

如果不是这样,请告诉我们您在尝试运行Unity时遇到了什么错误。

此外,在您的代码中,您不会将绕x轴的旋转限制在-89和89之间,因为您检查了错误的变量。rotation.x指的是四元数的x值,这可能不是您想要的。要检查被反对者的轮换数量,您应该使用:

if(transform.eulerAngles.x > -89f && transform.eulerAngles.x < 89f) 
{
transform.Rotate(moveVertical * rotationRate * Time.deltaTime, 0, 0); 
}

最新更新