Search Unity

Resolved How do i make my player model rotate in the direction of movement?

Discussion in 'Scripting' started by Sraigius, Dec 6, 2022.

  1. Sraigius

    Sraigius

    Joined:
    Oct 19, 2022
    Posts:
    1
    I am new to c# and coding in general and I was wondering how to make my player rotate to the direction of movement

    my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     [SerializeField] float movementSpeed = 5f;
    9.     [SerializeField] float jumpForce = 5f;
    10.     [SerializeField] Transform groundCheck;
    11.     [SerializeField] LayerMask ground;
    12.     [SerializeField] AudioSource jumpSound;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float horizontalInput = Input.GetAxis("Horizontal");
    23.         float verticalInput = Input.GetAxis("Vertical");
    24.  
    25.         rb.velocity = new Vector3(horizontalInput * movementSpeed, rb.velocity.y, verticalInput * movementSpeed);
    26.  
    27.  
    28.         if (Input.GetButtonDown("Jump") && IsGrounded())
    29.         {
    30.             Jump();
    31.         }
    32.      
    33.     }
    34.     void Jump()
    35.     {
    36.        
    37.         rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
    38.         jumpSound.Play();
    39.     }
    40.    private void OnCollisionEnter(Collision collision)
    41.     {
    42.         if (collision.gameObject.CompareTag("Enemy Head"))
    43.         {
    44.             Destroy(collision.transform.parent.gameObject);
    45.             Jump();
    46.         }
    47.     }
    48.     bool IsGrounded()
    49.     {
    50.         return Physics.CheckSphere(groundCheck.position, .1f, ground);
    51.     }
    52.    
    53. }
    54.  
     
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    So a couple of things before an answer.

    1) if you're new to unity and coding, you may want to control your player via altering its transform rather than physics. the reasoning being the considerations in the following points amongst other more advanced ones.
    2) When performing physics operations, it is best done in FixedUpdate and not Update, look into the difference, there's a lot of info available on it.
    3) you would still want to check your player's input in Update probably and save the values for using in FixedUpdate. You may notice jittery movement otherwise.
    4) Physics based movement (IN MY OPINION), is best reserved for when you dont want to make many claims about the objects position, speed, direction, etc, but rather simply enact forces on the object and let those values just be determined as a result of those forces.
    5) Once you control the object with physics, you can no longer reliably reference/alter its transform values. its not the biggest deal, as you can get that from the rigidbody component, but its easy to forget with other object referencing the object.
    6) you can harness some advantages of a rigidbody, like detecting contact with colliders, by setting the rigidbody isKinematic to true, while still performing movement through the tranform

    Now to answer your question, assuming your object's forward & up direction is the worlds forward & up direction when "not rotated":

    https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
    https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html

    Code (CSharp):
    1. Vector3 targetForwardDirection = velocity;
    2.  
    3. //get the rotation that corresponds to facing in the direction of the velocity
    4. Quaternion targetRotation = Quaternion.LookRotation(targetForwardDirection);
    5.  
    6. //explicity set the rotation of the rigidbody
    7. rb.MoveRotation(targetRotation);