2

Mathf.Clamp 在达到 maxValue 时将我的 eulerangle 重置为 minValue,而不是将其限制在 maxValue。我试过这个:

            rotateX = Input.GetAxis("Mouse X") * senseX;
        player.transform.Rotate(player.up, Mathf.Deg2Rad * rotateX, Space.World);

        playerXRotation = player.eulerAngles;

        while (playerXRotation.y > 180f)
            playerXRotation.y -= 360f;

        Debug.Log("Y Rotation: " + playerXRotation.y);
        playerXRotation.y = Mathf.Clamp(playerXRotation.y, lowLimitY, highLimitY);
        player.transform.eulerAngles = playerXRotation;

和这个 :

         rotate = Input.GetAxis("Mouse X") * senseX;
         rotation = player.transform.eulerAngles;
         rotation.y += rotate * RateOfRotate;

         while (rotation.y > 180)
         {
             rotation.y -= 360;
         }
         rotation.y = Mathf.Clamp(rotation.y, lowLimitY, highLimitY);
         player.transform.eulerAngles = rotation;

在这两种情况下,我的 lowLimitY = 0 和 highLimitY = 180;我被困在这个问题上,不知道如何解决它。任何帮助将不胜感激。

4

2 回答 2

2

因为旋转是四元数

并且它们被转换为范围为 [0-360) 的欧拉角,因此while (rotation.y > 180)永远不会起作用。

您需要单独跟踪旋转值,钳制到所需值,然后将其应用于对象的旋转。

rotation = player.transform.eulerAngles;
float rotationY = rotation.y + rotate * RateOfRotate;
rotationY = Mathf.Clamp(rotationY, lowLimitY, highLimitY);
rotation.y = rotationY;
player.transform.eulerAngles = rotation;
于 2018-10-30T00:52:27.143 回答
-1

如果你的

lowLimit = 0, 和highLimit = 180, 然后你运行:

while (rotation.y > 180)
{
  rotation.y -= 360; 
}

如果您的旋转是 maxValue (180),则 rotation.y 将等于 -180 (180 - 360 = -180)。

那么你的夹子是Mathf.Clamp(-180, 0, 180) // 0

如果您试图将 y 旋转保持在该范围内,那么删除 while 应该可以解决问题。如果您要获得不同的效果,我们将需要更多信息。

于 2018-10-29T20:57:03.327 回答