【问题标题】:Problem with Clamping Values for Object's Rotation - Unity C#对象旋转的钳位值问题 - Unity C#
【发布时间】:2019-04-14 03:21:17
【问题描述】:

我在夹紧枪管的度数加上其父(坦克的船体)度数时也遇到了问题,因此我可以得到与下图相同的结果。

所以脚本很简单,它使用 Y_Mouse 输入旋转桶(turretBarrel)并使用 X_Mouse 输入旋转坦克的上部(turret),以及枪管稳定。 脚本

   //Note this.transform is the tanks hull
  public Transform turret;
  public Transform turretBarrel;
  float xRot;
  float angleY;
  float rotY;
  float minTurretRotY;
  float maxTurretRotY;
  Vector3 mRot;
  Vector3 rot;


  public void Update(){
            xRot = Input.GetAxis("Mouse X") * 0.5f;
            turret.transform.localRotation *= Quaternion.Euler(turret.transform.localRotation.x, xRot, turret.transform.localRotation.z);

            angleY -= Input.GetAxisRaw("Mouse Y") * 0.5f;
            angleY = Mathf.Clamp(angleY, minTurretRotY, maxTurretRotY);

            mRot = turret.transform.rotation.eulerAngles;
            rot = turretBarrel.transform.rotation.eulerAngles;
            rotY = angleY;
            rotY = Mathf.Clamp(rotY, minTurretRotY, maxTurretRotY);

            // I'm using Quaternion.Euler for barrel Stabilization
            turretBarrel.transform.rotation = Quaternion.Euler(new Vector3(rotY, mRot.y, mRot.z));
            //Debug.Log(rotY +"  |  " + angleY + "/ Min : " + minTurretRotY+ " ;  Max : " + (maxTurretRotY));
}

【问题讨论】:

  • 改用mrot = turret.transform.localRotation.eulerAngles;
  • 那行不通。
  • 我真的不明白你在代码中做了什么。模型是否具有正确的层次结构?如果是这样,只需存储两个角度(并在输入时更新它们)并将炮塔和枪管的两个旋转设置为围绕相应轴(可能是 y 和 x)的纯旋转就足够了。

标签: c# unity3d math rotation


【解决方案1】:

您还需要根据主体(坦克)的 Y 旋转来更改最小和最大 Y。像这样更新:

minY = YRotationOfParent;
maxY = YRotationOfParent + someOffSet;

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多