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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cinemachine problems with first person controller

Discussion in 'Cinemachine' started by daisoto, Oct 10, 2021.

  1. daisoto

    daisoto

    Joined:
    Mar 28, 2020
    Posts:
    6
    Hello to everyone! I am writing a first person player controller and I am facing some problems with cinemachine (however, it may be general problems with engine usage). Both methods below (Move and Rotate) are called from Update() method.

    My cinemachine camera does not keep up with the movement of transform its attached on If it moves too fast (I am using CharacterController's Move method). Here are my cinemachine settings, moving method and how it looks (on capsule collider):
    cinemachine.png lookslike.png

    Code (CSharp):
    1. private void Move(Vector2 movingVector)
    2. {
    3.     Vector3 inputDirection = transform.right * movingVector.x +
    4.                           transform.forward * movingVector.y;
    5.  
    6.     Vector3 motion = inputDirection * movementSpeed *
    7.                      _currentRunMultiplier * Time.deltaTime;
    8.                    
    9.     _characterController.Move(motion);  
    10. }
    My second problem is twitching camera on rotation. I am using following method for rotating camera and it sometimes (it randomly depends on restarting the editor lol) twitches camera during the rotation.

    Code (CSharp):
    1. private void Rotate(Vector2 rotationVector)
    2. {
    3.     if (rotationVector.sqrMagnitude < _rotationThreshold) return;
    4.  
    5.     rotationVector *= rotationSpeed * Time.deltaTime;
    6.    
    7.     _verticalRotation = Mathf.Clamp(rotationVector.y + _verticalRotation,
    8.         _minVerticalRotation, _maxVerticalRotation);
    9.    
    10.     Vector3 headRotation = Vector3.right * _verticalRotation;
    11.     Vector3 bodyRotation = Vector3.up * rotationVector.x;
    12.    
    13.     _headTransform.localRotation = Quaternion.Euler(headRotation);
    14.     transform.Rotate(bodyRotation);
    15. }
    However, if I use coroutine for rotation following way, reacting on input manager:

    Code (CSharp):
    1. private void StartRotating()
    2. {
    3.     if (_rotationCoroutine != null) StopCoroutine(_rotationCoroutine);
    4.     _rotationCoroutine = Rotate();
    5.     StartCoroutine(_rotationCoroutine);
    6. }
    7.  
    8. private IEnumerator Rotate()
    9. {
    10.     while (_inputManager.rotation.Value != Vector2.zero)
    11.     {
    12.         Rotate(_inputManager.rotation.Value);
    13.        
    14.         yield return null;
    15.     }
    16. }
    17.  
    18. private void Rotate(Vector2 rotationVector)
    19. {
    20.     if (rotationVector.sqrMagnitude < _rotationThreshold) return;
    21.  
    22.     rotationVector *= rotationSpeed * Time.deltaTime;
    23.    
    24.     _verticalRotation = Mathf.Clamp(rotationVector.y + _verticalRotation,
    25.         _minVerticalRotation, _maxVerticalRotation);
    26.    
    27.     Vector3 headRotation = Vector3.right * _verticalRotation;
    28.     Vector3 bodyRotation = Vector3.up * rotationVector.x;
    29.    
    30.     _headTransform.localRotation = Quaternion.Euler(headRotation);
    31.     transform.Rotate(bodyRotation);
    32. }
    It also twitches, but much harder. Sometimes it vice versa rotating too slow. What am I doing wrong?
     
    Last edited: Oct 10, 2021
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,266
    You CM setup is wrong. You need separate game objects, not parented to each other:
    1. A Unity Camera game object with a CM Brain component
    2. Your character controller that moves and rotates on user input
    3. A CM virtual camera with a 3rdPerson setup, with the follow target being your character controller.
    See the AimingRig sample scene in the Cinemachine samples. You can also follow this tutorial:

     
    LuckyMisterSleven likes this.
  3. daisoto

    daisoto

    Joined:
    Mar 28, 2020
    Posts:
    6
    Thanks for your reply!

    I put them together only for screenshot, they are separated in my scene. Here is my hierarchy and those components:

    cameras.png


    AimingRig scene is using old input system, whereas I use new one. I am using starter assets` first person controller as reference, and my controller has almost the same business logic, but there is like some fundamental problem.

    By the way, calling Rotate method from LateUpdate removes camera twitching. My friend helped me, It is a shame I did not find it by myself.

    But I am still facing problem with virtual camera not keeping up with its following target's speed. By the way, default camera keeps up with the character controller`s movement (while cinemachine vitrual camera is not).
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,266
    Did you try setting the Damping to 0?

    upload_2021-10-14_15-36-45.png
     
    daisoto likes this.
  5. daisoto

    daisoto

    Joined:
    Mar 28, 2020
    Posts:
    6
    Thank you very much! You helped me a lot!
     
    Gregoryl likes this.