Search Unity

Question Cinemachine POV Look At issue

Discussion in 'Cinemachine' started by Auguin, Apr 28, 2021.

  1. Auguin

    Auguin

    Joined:
    May 23, 2018
    Posts:
    8
    Hi,

    I use a virtual camera on a Tracked Dolly and POV aim.
    I want to recenter the view automatically on an object (a cube), but for now the view always recenter on start orientation.
    I configured my virtual camera like the bottom screen.

    upload_2021-4-28_9-26-15.png

    Can someone as a solution to recenter on the cube and not the start orientation?

    Thanks for your help.
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Under POV, the recenter target option's Look At Target Forward mode is going to recenter the view, so that the camera forward is parallel and in the same direction as the Look At Target Forward.

    If you'd like to recenter so that the camera is looking at the box, then you could write a script that will orient the box in a way that it's forward is the same as the camera-box vector.

    For example, like in the image here:
    Screen Shot 2021-04-29 at 7.38.07 AM.png

    See the example script that will accomplish this below. To use this attach the script to your box, and add the vcam to the script. If you don't want to visually rotate the box, you could have an invisible child gameObject and rotate and look at that instead.
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3. public class RotateMeSoCameraLooksAtMe : MonoBehaviour
    4. {
    5.     public CinemachineVirtualCamera vcam;
    6.     void Update()
    7.     {
    8.         var vcamMeVector = transform.position - vcam.transform.position;
    9.         transform.rotation = Quaternion.LookRotation(vcamBoxVector);
    10.     }
    11. }
     
    Last edited: May 11, 2021
  3. Auguin

    Auguin

    Joined:
    May 23, 2018
    Posts:
    8
    Thanks for the solution !

    For others with the same issue, I just modify the script exemple to take the camera position instead of the virtual camera.
     
    gaborkb likes this.
  4. neviovalsa

    neviovalsa

    Joined:
    Jun 24, 2019
    Posts:
    52
    If the solution suggested above doesn't work for you, try this:
    (CurrentlySpectatingCamera is a CinemachineVirtualCamera reference)


    Code (CSharp):
    1.         /// <summary>
    2.         /// Rotates the current camera to face
    3.         /// </summary>
    4.         /// <param name="target"></param>
    5.         public void LookAt(Transform target)
    6.         {
    7.             var pov = CurrentlySpectatingCamera.GetCinemachineComponent<CinemachinePOV>();
    8.             if (pov != null)
    9.             {
    10.                 // Calculate the direction from the camera to the target
    11.                 var direction = target.position - CurrentlySpectatingCamera.transform.position;
    12.  
    13.                 // Calculate the rotation to look at the target
    14.                 var rotation = Quaternion.LookRotation(direction);
    15.  
    16.                 // Apply the rotation to the POV component
    17.                 pov.m_HorizontalAxis.Value = rotation.eulerAngles.y;
    18.                 pov.m_VerticalAxis.Value = rotation.eulerAngles.x;
    19.             }
    20.         }
    I like this better as it doesn't involve attaching a script to anything you want your camera to look at (and rotating it)