对象旋转的钳位值问题-Unity C#

时间:2018-11-11 00:05:07

标签: c# unity3d math rotation

我也无法确定炮管的度数和其父级(坦克的船体)度数,因此我可以获得与下图相同的结果。

因此脚本很简单,它使用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));
}

enter image description here

1 个答案:

答案 0 :(得分:0)

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

minY = YRotationOfParent;
maxY = YRotationOfParent + someOffSet;