1

I'm curently using cinemachine to make a third person camera. My player's movements work and i wanted him to look in the direction he is moving but taking in consideration the rotation of the camera. It works when i don't move the camera while moving the player but when i move the camera the rotations of my player are laggy moreover every movement of my camera seems to be laggy too. Sorry for my english i hope it's clear here is my code :

public class ThirdPersonScript : MonoBehaviour
 
 {
     [SerializeField] private float speed;
     [SerializeField] private float jumpForce;
     [SerializeField] private Transform feet;
     [SerializeField] private LayerMask floorMask;
     [SerializeField] private Transform cam;
 
     private Vector3 direction;
     private Rigidbody rb;
     private bool canJump;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         
         direction = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
         direction = Quaternion.AngleAxis(cam.rotation.eulerAngles.y, Vector3.up) * direction;
 
         if (direction.magnitude > 1.0f)
         {
             direction = direction.normalized;
         }
 
         direction *= speed;
 
         canJump = Input.GetKeyDown(KeyCode.Space) ? true : false;
     }
 
     void FixedUpdate()
     {
       
         if (Physics.CheckSphere(feet.position, 0.1f, floorMask))
         {            
             if (canJump)
                 rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
             else    
                 rb.velocity = new Vector3(direction.x, 0, direction.z);
         }
         else          
             rb.velocity = new Vector3(direction.x, rb.velocity.y, direction.z);
    
         if (direction != Vector3.zero)
         {
             Quaternion targetRotation = Quaternion.LookRotation(direction);
             targetRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 1080*Time.fixedDeltaTime);
             rb.MoveRotation(targetRotation);
 
         }
     }
 }

and a screenshot of my cinemachine settings and my hierarchy : enter image description here

Dusk
  • 33
  • 1
  • 8

1 Answers1

1

Try change the direction in LateUpdate() but not Update() :

 void LateUpdate()
 {
     
     direction = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
     direction = Quaternion.AngleAxis(cam.rotation.eulerAngles.y, Vector3.up) * direction;

     if (direction.magnitude > 1.0f)
     {
         direction = direction.normalized;
     }

     direction *= speed;

     canJump = Input.GetKeyDown(KeyCode.Space) ? true : false;
 }

LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update. https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

Infosunny
  • 459
  • 4
  • 17
  • It don't really change anything :/ maybe it is juste because i can't run the game at more than 30fps so the camera is a bit jerky – Dusk Oct 27 '21 at 13:26