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

How to make the player rotate and face the direction of movement when using AddForce (3rd Person)

Discussion in 'Scripting' started by AdityaSingh666, Mar 21, 2022.

  1. AdityaSingh666

    AdityaSingh666

    Joined:
    Jun 24, 2021
    Posts:
    2
    I use rb.AddForce in my rigidbody movement script to move the player, the only problem is, when the player moves, it doesn't rotate towards and face the direction it is moving in. For example, if the player is moving left, it will face the forward direction while moving left, which looks awkward. I'll insert a video.

    Is there any way to fix this? Please suggest me a solution, even if it requires me to change my movement code, but it has to be using rigidbodies only.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Data;
    4. using UnityEngine;
    5. using System;
    6. using ProwerEngine.Components;
    7. using ProwerEngine.Debugging;
    8. using UnityEngine.UI;
    9. using ProwerEngine.Player.Components;
    10. using UnityEngine.AI;
    11.  
    12. public class AltCharacterController : MonoBehaviour
    13. {
    14.     //Player Variables
    15.     private CharacterController controller;
    16.     protected Collider gCheck;
    17.     private Rigidbody rdbg;
    18.     [SerializeField] private float moveSpeed = 6f;
    19.     [SerializeField] private float jumpHeight = 6f;
    20.     private float moveHorizontal;
    21.     private float moveVertical;
    22.     private Vector3 PlayerMovementInput;
    23.     private Vector3 PlayerRotationInput;
    24.     private Vector3 MoveVector;
    25.  
    26.     //Camera Variable
    27.     public Transform cam;
    28.  
    29.     //Booleans
    30.     private bool groundedPlayer;
    31.  
    32.     //Animator
    33.     private Animator animator;
    34.  
    35.     void Start()
    36.     {
    37.         controller = GetComponent<CharacterController>();
    38.         rdbg = GetComponent<Rigidbody>();
    39.  
    40.         animator = GetComponent<Animator>();
    41.  
    42.         gCheck = GetComponent<Collider>();
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         //Input Function
    48.         MovementInput();
    49.  
    50.         //Grounded Functions
    51.         Grounded();
    52.  
    53.         Debug.Log(groundedPlayer);
    54.     }
    55.  
    56.     private void FixedUpdate()
    57.     {
    58.         //Movement Function
    59.         Movement();
    60.  
    61.         //Jump Function
    62.         Jumping();
    63.     }
    64.  
    65.     private void MovementInput()
    66.     {
    67.         moveHorizontal = Input.GetAxisRaw("Horizontal");
    68.         moveVertical = Input.GetAxisRaw("Vertical");
    69.  
    70.         PlayerMovementInput = Vector3.ClampMagnitude(new Vector3(moveHorizontal, 0, moveVertical), 1f);
    71.         MoveVector = transform.TransformDirection(PlayerMovementInput) * moveSpeed;
    72.     }
    73.  
    74.     private void Movement()
    75.     {
    76.         if (groundedPlayer)
    77.         {
    78.             rdbg.AddForce(MoveVector * moveSpeed, ForceMode.Acceleration);
    79.         }
    80.     }
    81.  
    82.     private void Jumping()
    83.     {
    84.         if (Input.GetButton("Jump") && groundedPlayer)
    85.         {
    86.             rdbg.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    87.         }
    88.     }
    89.  
    90.     //Checks if player is touching ground/platform
    91.     private void Grounded()
    92.     {
    93.         groundedPlayer = Physics.Raycast(transform.position, Vector3.down, gCheck.bounds.extents.y + 0.1f);
    94.  
    95.         if(groundedPlayer)
    96.         {
    97.             animator.SetBool("Falling", false);
    98.         }
    99.     }
    100.  
    101.     private void CanJump()
    102.     {
    103.  
    104.     }
    105.  
    106. }
    107.  
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    There is nothing in your code about the rotation of your character so of course it will not rotate (exept with the built in physics if it collide with someting etc.. ).

    You can't handle your player rotation directly with rigidbodies, only torque. Try this to make your character face the direction he is going. you will probably need to handle the case where the speed is or close to 0.
    Code (CSharp):
    1. transform.rotation = Quaternion.LookRotation(rdbg.velocity.normalized, Vector3.up);
     
  3. AdityaSingh666

    AdityaSingh666

    Joined:
    Jun 24, 2021
    Posts:
    2
    I tried this, but it doesn't work as expected. There's a horrible jitter and the player would start to go around in circles if I press the left or right keys.

     
    Last edited: Mar 22, 2022
  4. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Like i said, you need to handle the case where the speed is or close to 0. You can see the jitter occurs only at very low speed. Also you can avoid using the Y part of your velocity (vertical speed) to handle the jump case.