C# 尝试使用LocalEulerAngles旋转时游戏对象的未知旋转行为
我是游戏开发新手,我正在遵循曼宁的《行动中的统一》一书。这是用于旋转游戏对象的代码段(来自书本): 这段代码实际上是有效的,但反过来说,当我向上移动鼠标时,当我向下移动时,对象会上下旋转。我发现它是C# 尝试使用LocalEulerAngles旋转时游戏对象的未知旋转行为,c#,unity3d,C#,Unity3d,我是游戏开发新手,我正在遵循曼宁的《行动中的统一》一书。这是用于旋转游戏对象的代码段(来自书本): 这段代码实际上是有效的,但反过来说,当我向上移动鼠标时,当我向下移动时,对象会上下旋转。我发现它是rotationX-=Input.GetAxis(“鼠标Y”)*sensitivityVert这是导致问题的原因,因此我将其更改为rotationX+=Input.GetAxis(“鼠标Y”)*sensitivityVert
rotationX-=Input.GetAxis(“鼠标Y”)*sensitivityVert代码>这是导致问题的原因,因此我将其更改为rotationX+=Input.GetAxis(“鼠标Y”)*sensitivityVert现在它的旋转真的很奇怪!我已经确保它只在垂直轴上旋转,但它在所有方向上旋转,即在水平轴和z轴上旋转。
谁能告诉我哪里出了问题?谢谢你对我来说很好。我清理了您的代码并将Y约束为其初始值
public class rotateX : MonoBehaviour
{
public float sensitivityVert = 9.0f;
private float rotationX = 0;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
Vector3 initial;
void Start()
{
initial = transform.localEulerAngles;
}
// OnMouseDown is called when the user has pressed the
// mouse button while over the GUIElement or Collider.
protected void OnMouseDown()
{
sign = -sign;
}
float sign = -1f;
void Update()
{
rotationX += sign * Input.GetAxis("Mouse Y") * sensitivityVert;
//rotationX = Mathf.Clamp(rotationX, minimumVert, maximumVert);
transform.localEulerAngles = new Vector3(rotationX, initial.y, 0f);
}
}
立方体围绕其轴心上下旋转。您尝试过使用sharpgl旋转吗?不,我想这样做。您是从向量开始工作的。零旋转吗?或者您在尝试旋转的对象上是否有一些默认旋转。如果(axes==RotationAxes.MouseY)返回false。请简化你的代码也为我工作!但是由于rotationX-=Input.GetAxis(“鼠标Y”)*灵敏度的缘故代码>当我向上移动鼠标时,游戏对象向下看,当我向下移动鼠标时,对象向上看!!怎么办?我现在没有打开unity,但是您可以在Vector3
赋值中使用-rotationX
,就像这样transform.localEulerAngles=new Vector3(-rotationX,initial.y,0f)代码>这应该会翻转标志。是的,在我发布这个问题之前,我已经尝试了很久了!当我这样做的时候,旋转的动作非常奇怪,好吧,不要这样做。让代码保持原样,或者像我刚才那样注释掉钳子,看看它是否工作正常。走到立方体的另一侧,看到它在向上看时向上旋转,在向下看时向下旋转。或者,如果您无法移动,请将-=
更改为+=
,但如果您可以移动并且位于多维数据集的错误一侧,则会产生相同的影响。@MR.JOIS在旋转脚本中添加了一个单击处理程序。现在可以单击立方体以更改旋转方向。确保立方体(对象)上有一个活动的碰撞器。
public class rotateX : MonoBehaviour
{
public float sensitivityVert = 9.0f;
private float rotationX = 0;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
Vector3 initial;
void Start()
{
initial = transform.localEulerAngles;
}
// OnMouseDown is called when the user has pressed the
// mouse button while over the GUIElement or Collider.
protected void OnMouseDown()
{
sign = -sign;
}
float sign = -1f;
void Update()
{
rotationX += sign * Input.GetAxis("Mouse Y") * sensitivityVert;
//rotationX = Mathf.Clamp(rotationX, minimumVert, maximumVert);
transform.localEulerAngles = new Vector3(rotationX, initial.y, 0f);
}
}