Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Cinemachine camera lagging behind player and randomly moving around.

Discussion in 'Getting Started' started by minelad, Mar 4, 2023.

  1. minelad

    minelad

    Joined:
    Jun 21, 2020
    Posts:
    1
    I have been having a problem with Cinemachine where my freelook camera will be further behind the player while the player is moving away from it, which I do not want. Also, the camera sometimes randomly flashes another perspective for about a frame, which is very jarring. Is there any way I can fix this? I have been searching online and haven't found anything that relates to my problem. My cinemachine brain is set to late update so that isn't the problem.

    here is my cinemachine camera's settings (the rig settings aren't accurate because I have a custom script to change them)
    upload_2023-3-4_12-28-58.png
    upload_2023-3-4_12-29-33.png
    upload_2023-3-4_12-30-18.png
    (sorry for windows 10 watermark)

    here is my script for changing camera zoom:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CameraZoom : MonoBehaviour
    7. {
    8.  
    9.     public Cinemachine.CinemachineFreeLook cam;
    10.     private float zoom = 5;
    11.     private float sensitivity = 1;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         zoom += Input.mouseScrollDelta.y * sensitivity;
    17.  
    18.         zoom = Mathf.Clamp(zoom, 3, 30);
    19.  
    20.         cam.m_Orbits[1].m_Radius = zoom;
    21.         cam.m_Orbits[0].m_Height = zoom;
    22.         cam.m_Orbits[2].m_Height = -zoom;
    23.  
    24.     }
    25. }
    26.  
    here is my code for player movement:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.     public Transform character;
    10.  
    11.     public float speed = 6f;
    12.  
    13.     Vector3 moveVector;
    14.     bool isJumping = false;
    15.     float timeSinceJump = 0f;
    16.     Vector3 jumpVector;
    17.  
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.  
    23.         bool isGrounded = Physics.Raycast(character.position, Vector3.down, 1.1f);
    24.  
    25.         moveVector = Vector3.zero;                                              // prevent building speed every frame
    26.  
    27.         transform.rotation = Quaternion.Euler(0, cam.eulerAngles.y, 0);         // make the player always face away from the camera
    28.  
    29.         Vector3 forward = (transform.rotation * Vector3.forward).normalized;    // determine which direction is forward
    30.         Vector3 camDir = (cam.rotation * Vector3.forward).normalized;           // determine which direction the camera is facing
    31.  
    32.         Vector3 right = Vector3.Cross(forward,camDir).normalized;               // find vector perpendicular to camDir and forward to find which way is right
    33.  
    34.         // make player move in the correct direction
    35.         if (Input.GetKey(KeyCode.W))
    36.         {
    37.             moveVector += forward;
    38.         }
    39.         if (Input.GetKey(KeyCode.S))
    40.         {
    41.             moveVector += forward * -1f;
    42.         }
    43.         if (Input.GetKey(KeyCode.D))
    44.         {
    45.             moveVector += right;
    46.         }
    47.         if (Input.GetKey(KeyCode.A))
    48.         {
    49.             moveVector += right * -1f;
    50.         }
    51.         if (Input.GetKey(KeyCode.Space) && isGrounded)
    52.         {
    53.             isJumping = true;
    54.         }
    55.  
    56.         if (timeSinceJump >= 0.5)
    57.         {
    58.             isJumping = false;
    59.             timeSinceJump = 0f;
    60.         }
    61.         if (isJumping)
    62.         {
    63.             jumpVector = Vector3.up * 10 - Vector3.up * timeSinceJump * 10 * 2;
    64.             timeSinceJump += Time.deltaTime;
    65.         } else
    66.         {
    67.             jumpVector = Vector3.zero;
    68.         }
    69.  
    70.         if (!isGrounded && !isJumping)
    71.         {
    72.             controller.Move(Physics.gravity * Time.deltaTime);
    73.         }
    74.  
    75.         controller.Move(moveVector.normalized * speed * Time.deltaTime + jumpVector * Time.deltaTime);        // move player
    76.  
    77.  
    78.  
    79.        
    80.     }
    81. }
    82.  
    I don't know where the problem might be so I just put everywhere that I thought could be the problem.
     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,905
    Sorry for the slow response. Do your want your camera to be at a fixed distance from the player, regardless of how fast the player is moving? If so, set the Z damping to 0 in the Orbital Transposer (open up the "Body" section of the FreeLook to find the orbital transposer damping settings). Do it for all 3 rigs.

    upload_2023-3-14_11-12-14.png