【问题标题】:Unity Rotation Clamping IssueUnity 旋转夹紧问题
【发布时间】:2020-08-01 16:59:46
【问题描述】:

我正在尝试实现一个系统,在该系统中,我的相机将放大游戏对象的正面,然后我可以通过夹紧来围绕它旋转。

我已经设法让它工作了,虽然我猜我做错了 - 基本上我希望实际角度在我向左平移时是负的,而当我向右平移时是正的。

它适用于没有旋转的目标,但不适用于有旋转的目标,因为中心角不为零。

这是我的功能:

float _prevMousePosX;

 void RotateCamera()
 {
 //Allow min -30 degrees when panning to the left
 minXLimit = -50;
 //And max 30 degrees when panning to the right
 maxXLimit = 50;
 //Work out how much the mouse has moved
 var mouseX = Input.GetAxis("Mouse X");

 Vector3 angle = target.position + target.rotation * transform.position * -1;
 var actualAngle = angle.x;
 //The angle is very small so times it by 100
 actualAngle = actualAngle * 100;

 //This angle is not defaulting to zero before any rotation has occurred when the game object has a rotation other than zero - I think I should be taking into account the existing rotation somehow
 Debug.Log(actualAngle);

 //The rest of this code is working correctly
 if ((actualAngle > minXLimit && actualAngle < maxXLimit && isMoving))
 {
     //No - Allow rotating in either direction horizontally
     if(isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
 } else
 {
     //Yes, it's more than 30 degrees either left or right
     //Work out which way wer're trying to drag the mouse and whether we should allow rotation further in that direction
     if (mouseX>0 && actualAngle < minXLimit)
     {
         //Don't allow?
     } 
     else if (mouseX<0 && actualAngle < minXLimit)
     {
         //Allow rotating back
         if (isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
     }
     else
     {
         //Mouse isn't moving
     }

     if (mouseX>0 && actualAngle > maxXLimit)
     {
         //Allow rotating back
         if (isMoving) transform.RotateAround(target.transform.position, Vector3.up, mouseX * 1);
     }
     else if(mouseX<0 && actualAngle > maxXLimit)
     {
         //Don't allow rotating in this direction any further
     } else
     {
         //Mouse isn't moving
     }
 }
 _prevMousePosX = mouseX;

}

【问题讨论】:

    标签: unity3d rotation


    【解决方案1】:

    问题已解决。

        //Work out how close we are to facing each other
        //We can minus 180 because we'll always be facing each other, but just not always directly
        var newAngle = Quaternion.Angle(target.rotation, transform.rotation) -180;
        //Then we need to work out whether we are to it's left or it's right
        float rotateDirection = (((transform.eulerAngles.y - target.eulerAngles.y) + 360f) % 360f) > 180.0f ? 1 : -1;
    
        var actualAngle = newAngle;
        //And finally we can negate our degrees if we're to it's left
        actualAngle = actualAngle * rotateDirection;
    

    然后你可以像上面一样使用actualAngle来限制你的旋转。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 2021-06-08
      • 1970-01-01
      相关资源
      最近更新 更多