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

Question Cinemachine Look At not working

Discussion in 'Cinemachine' started by ts_viana, Jun 27, 2023.

  1. ts_viana

    ts_viana

    Joined:
    Oct 1, 2020
    Posts:
    4
    Hello! So, I'm trying to fix this issue for two days but no success so far. I'll try to provide as much information below as possible.

    So, I'm creating a game where the camera switches from 3rd person to an static camera depending on the situation. This situation can be called via code or via a trigger on the scene. The default is always the 3rd person camera. This system has been created using Cinemachine and most of it is working, except for the look at that does not respond and does not shows any errors on the console. Here's how the system works.

    First, we have a Camera Manager object that collects the cameras on each new scene on a list, and uses functions to activate of deactivate them as necessary:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CameraManager : MonoBehaviour
    7. {
    8.     public List<CinemachineVirtualCamera> sceneCameras = new List<CinemachineVirtualCamera>();
    9.  
    10.     public void LoadSceneCameras()
    11.     {
    12.         sceneCameras.Clear();
    13.  
    14.         Transform sc = SceneCameraInstance.instance.transform;
    15.         foreach(Transform child in sc)
    16.         {
    17.             sceneCameras.Add(child.GetComponent<CinemachineVirtualCamera>());
    18.         }
    19.  
    20.         foreach(CinemachineVirtualCamera cm in sceneCameras)
    21.         {
    22.             cm.Priority = 5;
    23.         }
    24.     }
    25.  
    26.     public void SetActiveCamera(CinemachineVirtualCamera vc)
    27.     {
    28.         for(int i = 0; i < sceneCameras.Count; i++)
    29.         {
    30.             if(sceneCameras[i] == vc)
    31.             {
    32.                 vc.Priority = 10;
    33.                 vc.gameObject.GetComponentInChildren<VirtualCameraPlayerReference>().Setup();
    34.                 PlayerInstance.instance.GetComponentInChildren<CinemachineFreeLook>().enabled = false;
    35.                 break;
    36.             }
    37.         }
    38.     }
    39.  
    40.     public void DeactivateCamera(CinemachineVirtualCamera vc)
    41.     {
    42.         for(int i = 0; i < sceneCameras.Count; i++)
    43.         {
    44.             if(sceneCameras[i] == vc)
    45.             {
    46.                 vc.Priority = 5;
    47.                 PlayerInstance.instance.GetComponentInChildren<CinemachineFreeLook>().enabled = true;
    48.                 break;
    49.             }
    50.         }
    51.     }
    52. }
    This script is working fine and the cameras are added to the list as expected and their priority is changed normally. However, the activated virtual camera does not look at the target I've set, independently of the object. I have another script commanding this reference, which is activated via the Setup function seen on the code above. The Setup function is this (so I can select if the camera should follow the player or not):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class VirtualCameraPlayerReference : MonoBehaviour
    7. {
    8.     [SerializeField] bool followPlayer = false;
    9.     [SerializeField] Transform lookAtObject;
    10.  
    11.     public void Setup()
    12.     {
    13.         if(followPlayer)
    14.         {
    15.             GetComponent<CinemachineVirtualCamera>().LookAt = PlayerInstance.instance.transform;
    16.         }
    17.         else
    18.         {
    19.             GetComponent<CinemachineVirtualCamera>().LookAt = lookAtObject;
    20.         }
    21.     }
    22. }
    However, the camera does not follow the player or look at the target no matter what I do, it just sits there with the camera's position and rotation. Here's the prefab I'm using to set the cameras on the scene:
    upload_2023-6-27_12-51-15.png

    Has anybody ever encountered this? I'll let a video below showing what's up. Thanks for any help!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Your prefab does not have any procedural motion selected, so it will just sit there. You have to set Aim and Body to something active. "Do Nothing" will do nothing.

    upload_2023-6-27_12-1-50.png
     
  3. ts_viana

    ts_viana

    Joined:
    Oct 1, 2020
    Posts:
    4
    Yes, I've tried to do this before, but then another weird behaviour happens:


    I've tried with all options on the Aim. I figured I don't need anything on the Body since I don't want the camera to change positions, just to aim. Should I select it for both or it won't work?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    "Body" affects camera position, "Aim" affects camera rotation, so no you should leave Body on Do Nothing if that's what you want.

    Don't just randomly select all the options, decide what you want and choose the appropriate thing. If you want a hard look-at, choose "Hard Look At". If you want a softer lookAt with screen composition and damping, choose "Composer".

    Check out the user manual here: https://docs.unity3d.com/Packages/com.unity.cinemachine@2.9/manual/index.html
     
  5. ts_viana

    ts_viana

    Joined:
    Oct 1, 2020
    Posts:
    4
    Yes, that did it. I had some issues with the prefab as well, but everything is working as expected now. Thanks!
     
    Gregoryl likes this.