Search Unity

Rotate player always in the same direction

Discussion in 'Physics' started by Farravid, Aug 13, 2020.

  1. Farravid

    Farravid

    Joined:
    Jul 12, 2019
    Posts:
    9
    Hi! I've been working with rotations animations and other stuff in unity.
    I want to always rotate my player in the same direction. How can I do that?
    My problem is if Y value of rotation is negative, player will rotate toward left, however if Y is postive he will rotate toward right. This is my code. Thank u everyone.

    Code (CSharp):
    1. bool rotationAllowed = (m_GolemInput.MoveInput.sqrMagnitude > k_MinRotacion) ? true : false;
    2.  
    3.         //Clmaping magnitude
    4.         Vector3 playerInput;
    5.         playerInput = Vector3.ClampMagnitude(new Vector3(m_GolemInput.MoveInput.x, 0f, m_GolemInput.MoveInput.y), 1);
    6.  
    7.         Vector3 camForw = Camera.main.transform.forward;
    8.         Vector3 camRight = Camera.main.transform.right;
    9.  
    10.         camForw.y = 0f;
    11.         camRight.y = 0f;
    12.  
    13.         camForw.Normalize();
    14.         camRight.Normalize();
    15.  
    16.         m_DesiredMovement = playerInput.x * camRight + playerInput.z * camForw;
    17.         m_DesiredMovement = m_DesiredMovement * m_RealMoveSpeed;
    18.  
    19.         //Calculamos el angulo de giro para activar o no la animacion
    20.         float anglesDifToTurn = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(m_DesiredMovement));
    21.  
    22.         if (Math.Abs(m_GolemInput.MoveInput.x) > Math.Abs(m_GolemInput.MoveInput.y))
    23.             anglesDifToTurn = m_GolemInput.MoveInput.x > 0 ? anglesDifToTurn * 1 : anglesDifToTurn * -1;
    24.         else
    25.             anglesDifToTurn = m_GolemInput.MoveInput.y > 0 ? anglesDifToTurn * 1 : anglesDifToTurn * -1;
    26.  
    27.         if (!IsMoveInput)
    28.             anglesDifToTurn = 0f;
    29.  
    30.         m_Animator.SetFloat(m_HashRadianesGiro, anglesDifToTurn);
    31.  
    32.  
    33.         if (rotationAllowed)
    34.             transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(m_DesiredMovement)), rotateAnimationSpeed * m_Delta);
    35.  
    36.         m_CharCtrl.Move(m_DesiredMovement * m_Delta);