Search Unity

Question What is the best way to control CM position and the Recenter Offset?

Discussion in 'Cinemachine' started by Patrick_Machado_, Mar 29, 2023.

  1. Patrick_Machado_

    Patrick_Machado_

    Joined:
    Feb 1, 2018
    Posts:
    67
    I've a project that I'm working on, where I have a 3rd person character movement, for the camera I'm using a CM Virtual Machine, my problem is actually that I want the both Recentering functions of Virtual Machine camera (X and Y recentering) to lead the camera to the position behind the characters back, right now it's at a global position, related to the world.

    In a brief, my problem is the camera recentering to a fixed world position and the character be facing a direction and the camera is not on the character default back view position, on horizontal (X Recentering). I've tried two scripts which I'll let their respective methods of my tries below, they didn't work.

    So what is the bast way to change the default position the camera should recenter to related to the characters 3rd person back instead of the world related fixed position?

    Method of try #1:
    Code (CSharp):
    1. private void PositionCameraBehindCharacter()
    2.     {
    3.         if (transposer == null) return;
    4.  
    5.         Vector3 relativePosition = target.InverseTransformPoint(doorFrameCamera.transform.position);
    6.  
    7.         float targetDistance = Mathf.Lerp(relativePosition.z, -distanceBehindCharacter, Time.deltaTime * 5.0f);
    8.         Vector3 targetOffset = Vector3.Lerp(transposer.m_FollowOffset, new Vector3(0, cameraOffset.y, targetDistance), Time.deltaTime * 5.0f);
    9.  
    10.         transposer.m_FollowOffset = targetOffset;
    11.     }
    Method of try #2:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         // Get the default recentering position for the camera
    4.         Vector3 defaultRecenterPosition = _virtualCamera.State.FinalPosition;
    5.  
    6.         // Calculate the target position for the camera with an offset behind the character
    7.         Vector3 targetPosition = _target.position - _target.forward * _distance + Vector3.up * _height - _target.right * _recenterOffset;
    8.  
    9.         // Smoothly move the camera towards the target position
    10.         transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * _damping);
    11.  
    12.         // Rotate the camera to look at the target
    13.         transform.LookAt(_target);
    14.  
    15.         // Set the default recentering position to the offset position behind the character
    16.         _composer.m_TrackedObjectOffset = Vector3.right * _recenterOffset;
    17.     }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Can you show a picture of your VirtualCamera inspector?
     
  3. Patrick_Machado_

    Patrick_Machado_

    Joined:
    Feb 1, 2018
    Posts:
    67
    My apologies, attached below are the respective CineMachine camera prints from the inspector, I also attached a desired point of view I want the camera to always back when recentenring (Vertical and Horizontal) regardless the character direction.
     

    Attached Files:

  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Try this:

    upload_2023-3-29_16-19-49.png
     
  5. Patrick_Machado_

    Patrick_Machado_

    Joined:
    Feb 1, 2018
    Posts:
    67
    Can't believe it was this simple! It solved, thank you very much!
     
    Gregoryl likes this.