Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Rotation Issue

Discussion in 'Physics' started by Gamer98898, Oct 6, 2021.

  1. Gamer98898

    Gamer98898

    Joined:
    Nov 18, 2017
    Posts:
    11
    Hie everyone I want to rotate my character like in this video

    Here is the link



    Code (CSharp):
    1. RotationValues= new vector3(0,95,0);
    2.  
    3. void Rotate()
    4.  
    5.     {
    6.    
    7.         quaternionA = Player.transform.rotation;
    8.      
    9.       Player.transform.rotation=  Quaternion.Slerp(quaternionA, Quaternion.Euler(RotationValues), Rotatingvalue);
    10.      
    11.       Rotatingvalue += Time.deltaTime;
    12.  
    13.    
    14.  
    15.      
    16.     }
    Please help me ,
    Thanks for in advance
     
    Last edited: Oct 6, 2021
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,116
    This doesn't seem to be physics-related, just some basic transform rotation. And if you want to do the same thing you see here, turning only 90 degrees left or right, I'd say a simple Coroutine using euler angles would be fine. It seems the camera is a child of the player, so rotating the player alone should be fine. Something like this. I haven't testing this, but I imagine it's probably a reasonable starting point:

    Code (CSharp):
    1. public float TurningSpeed = 1;
    2. public void PerformTurn(bool turnLeft)
    3. {
    4.     StartCoroutine(TurnRoutine(turnLeft));
    5. }
    6.  
    7. private IEnumerator TurnRoutine(bool turnLeft)
    8. {
    9.     var currentY = Player.transform.localEulerAngles.y;
    10.     var finalY = currentY;
    11.     if (turnLeft)
    12.     {
    13.         finalY -= 90;
    14.     } else
    15.     {
    16.         finalY += 90;
    17.     }
    18.  
    19.     float t = 0;
    20.     while (t < 1)
    21.     {
    22.         t += Mathf.Min(1f, Time.deltaTime * TurningSpeed);
    23.         var newY = Mathf.Lerp(currentY, finalY, t); // Or Slerp if you prefer.
    24.         Player.transform.localEulerAngles = new Vector3(0, newY, 0);
    25.         yield return null;
    26.     }
    27. }
    You'll call the PerformTurn method when you want to do a turn, passing whether to turn left or right. You can adjust TurningSpeed to control how fast it goes.
     
    Gamer98898 likes this.
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    You multiply rotations to achieve the "addition". The sign should be *= and not +=.
     
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Problem is Eulers introduce gimbal lock at orthogonal angles as well as that a euler repesentation of an angle can be multiple actual orientations or ways to arrive at.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,116
    That's true, but as I mentioned, it seems they're just rotating on the y-axis, so there would be no gimbal lock. And the code I posted wouldn't be affected by the fact that y=-180 is the same as y=180. So, in this use case, I don't think there's any advantage to trying to use quaternions over using euler angles.

    But that's entirely based on reproducing the example provided in the video. If you're going to be other, more complex rotations, then you'd need a more robust solution. But, if you're just going to rotate on y, as in the video, I personally wouldn't bother.
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Quats will find the closest unambiguous rotation. Quaternion.Euler is a good friend when yer "cornered' so to speak.
     
  7. Gamer98898

    Gamer98898

    Joined:
    Nov 18, 2017
    Posts:
    11
    Thanks for help,
    if you see gameplay video properly Character is rotating 90degree its right but while moving in middle or sometime in corner of the path it is rotating in a arc or may be in curved path .