[英]Problem with Clamping Values for Object's Rotation - Unity C#
我在夹紧枪管的度数加上它的父级(坦克的船体)度数时也遇到了麻烦,所以我可以获得与下图右侧相同的结果。
所以脚本很简单,它使用 Y_Mouse 输入旋转枪管 ( turretBarrel ) 并使用 X_Mouse Input 旋转坦克的 upperPart( 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));
}
您还需要根据主体(坦克)的 Y 旋转来更改最小和最大 Y。 像这样更新:
minY = YRotationOfParent;
maxY = YRotationOfParent + someOffSet;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.