How can I prevent my game object from rotating all the time when trying to move it

So, I have a game object (my player) that moves through the joystick. Without the spinning code below, my player moves well and smoothly, but after I put the spinning code below the game object, it rotates all the time (360 degrees) when I try to move it.

My goal is to rotate the player around a specific angle as the player turns.

void Update()
{
    // move
    _rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
        (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );

    //rotate 
    double rad = Mathf.Atan2(leftController.GetTouchPosition.y, leftController.GetTouchPosition.x); // In radians
    double deg = rad * (180 / System.Math.PI);
    transform.RotateAround(transform.position, Vector3.up * Time.deltaTime, (float) deg);
}

      

+3


source to share


1 answer


Correct me if I am wrong, but if I have the right question, I think you are using the wrong function.

For me RotateAround would be better for planet orbit like (perpetual motion)



If you just want to turn the player in a given direction and stop when done, you should try the following functions:

  • Transform.LookAt
  • Quaternion.LookRotation
  • Quaternion.RotateTowards
  • Quaternion.Slerp
+2


source







All Articles