Проблема фиксации вращения Unity - PullRequest
0 голосов
/ 19 апреля 2020

Я пытаюсь создать систему, в которой моя камера будет увеличивать переднюю поверхность игрового объекта, и затем я могу вращаться вокруг него с помощью зажимов.

Мне удалось заставить его работать, хотя Я предполагаю, что я сделал это совершенно неправильно - в основном я ожидаю, что actualAngle будет ОТРИЦАТЕЛЬНЫМ, когда я перемещаюсь влево, и ПОЛОЖИТЕЛЬНО, когда я поворачиваюсь вправо.

Это работает для целей, которые не имеют вращение, но не для целей, у которых есть поворот, поскольку центрированный угол не равен нулю.

Вот моя функция:

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;

}

1 Ответ

1 голос
/ 19 апреля 2020

Проблема решена.

    //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, чтобы зафиксировать вращение, как я делал выше.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...