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

New scene, new camera, following the same character

Discussion in 'Cinemachine' started by kstrous, Oct 30, 2021.

  1. kstrous

    kstrous

    Joined:
    Oct 26, 2021
    Posts:
    7
    hi there, I recently started working on my own game and what I'd like to do is exactly what the title suggests. Right now I have my game setup with a base scene which never unloads where I keep my player and my camera. The problem with this is that I can't set confines for the camera depending on which room/scene the player is in, so I'd like to use a different camera for each scene. But I can't set these cameras to follow the player which I keep in my base scene.
    Is there a solution for this? I've done a lot of research but I couldn't really find anything, it seems cross-scene references is one of Unity's weaker points, that said I know that something like this should be possible as this is what Hollow Knight(game which was built in unity) does.
     
  2. MineCode27

    MineCode27

    Joined:
    Dec 27, 2013
    Posts:
    9
    Hi, you will need to set refference for follow traget when new camera loads. So I suggest you that you put some custom script on your camera and when camera first loads you assign your player to follow refference
    Code (CSharp):
    1. vc.m_Follow = GameObject.FindGameObjectWithTag( "Player" ).transform;
    .
     
    Gregoryl and gaborkb like this.
  3. kstrous

    kstrous

    Joined:
    Oct 26, 2021
    Posts:
    7
    Thank you that worked!
    Here's the exact code I used in case anyone has the same problem

    Code (CSharp):
    1. public class CameraScript : MonoBehaviour
    2. {
    3.     private CinemachineVirtualCamera vcam;
    4.     void Start()
    5.     {
    6.         var vcam = GetComponent<CinemachineVirtualCamera>();
    7.         vcam.Follow = GameObject.FindGameObjectWithTag("Player").transform;
    8.     }
    9.  
    10. }