Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.
Forum begins after the advertisement:
Part 6: New camera won’t follow player
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › Part 6: New camera won’t follow player
- This topic has 6 replies, 3 voices, and was last updated 2 weeks, 5 days ago by
Terence.
-
AuthorPosts
-
October 16, 2025 at 7:01 am #18924::
I have been trying to follow the camera section but I’m having great difficulty considering the fact that the audio and video are not synced up. The problem I am having is that my new camera will NOT follow my player.
View post on imgur.com
October 16, 2025 at 11:31 am #18925October 16, 2025 at 8:20 pm #18930::Camera Manager:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; public class CameraManager : MonoBehaviour { [SerializeField] CinemachineVirtualCamera[] allVirtualCameras; private CinemachineVirtualCamera currentCamera; private CinemachineFramingTransposer framingTransposer; public static CameraManager Instance { get; private set; } private void Awake() { if (Instance == null) { Instance = this; } for (int i = 0; i < allVirtualCameras.Length; i++) { if (allVirtualCameras[i].enabled) { currentCamera = allVirtualCameras[i]; framingTransposer = currentCamera.GetCinemachineComponent<CinemachineFramingTransposer>(); } } } private void Start() { for (int i = 0; i < allVirtualCameras.Length; i++) { allVirtualCameras[i].Follow = PlayerController.Instance.transform; } } public void SwapCamera(CinemachineVirtualCamera _newCam) { currentCamera.enabled = false; currentCamera = _newCam; currentCamera.enabled = true; } }Camera Trigger:
using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; public class CameraTrigger : MonoBehaviour { [SerializeField] private CinemachineVirtualCamera newCamera; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { CameraManager.Instance.SwapCamera(newCamera); } } }October 16, 2025 at 9:56 pm #18932::@Alp can you see any issues with these 2 pieces of code that are preventing the camera from following the player?
using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; public class CameraManager : MonoBehaviour { [SerializeField] CinemachineVirtualCamera[] allVirtualCameras; private CinemachineVirtualCamera currentCamera; private CinemachineFramingTransposer framingTransposer; public static CameraManager Instance { get; private set; } private void Awake() { if (Instance == null) { Instance = this; } for (int i = 0; i < allVirtualCameras.Length; i++) { if (allVirtualCameras[i].enabled) { currentCamera = allVirtualCameras[i]; framingTransposer = currentCamera.GetCinemachineComponent<CinemachineFramingTransposer>(); } } } private void Start() { for (int i = 0; i < allVirtualCameras.Length; i++) { allVirtualCameras[i].Follow = PlayerController.Instance.transform; } } public void SwapCamera(CinemachineVirtualCamera _newCam) { currentCamera.enabled = false; currentCamera = _newCam; currentCamera.enabled = true; } }using System.Collections; using System.Collections.Generic; using UnityEngine; using Cinemachine; public class CameraTrigger : MonoBehaviour { [SerializeField] private CinemachineVirtualCamera newCamera; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { CameraManager.Instance.SwapCamera(newCamera); } } }October 16, 2025 at 10:04 pm #18933::Looking at your code, there are a couple of potential issues that might be preventing the camera from properly following the player:
-
Camera Initialization: In the
CameraManagerclass, thecurrentCamerais set in theAwakemethod based on which camera is enabled. If none of the cameras inallVirtualCamerasare enabled at the start (or ifPlayerController.Instanceis not set properly),currentCameramay remain null, which could lead to issues when trying to swap cameras or setting theFollowtarget. -
Camera Follow Target: In the
Startmethod, you set theFollowproperty of all virtual cameras to the player. However, ifSwapCamerais called and the new camera (newCamera) does not have itsFollowtarget set to the player, it won’t follow the player. You might want to ensure thatnewCamera.Followis also assigned toPlayerController.Instance.transformwhen you swap cameras. -
Camera Activation: In the
SwapCameramethod, you disable the current camera without checking if it is null. IfcurrentCamerais null, trying to accessenabledwill throw a null reference exception. You should add a check to ensurecurrentCamerais not null before trying to disable it. -
Cinemachine Component: If you are relying on
CinemachineFramingTransposer, ensure that it is correctly set up in your virtual camera. If not configured properly, it might not work as expected.
Here’s an updated version of your
SwapCameramethod that includes checks and ensures the new camera follows the player:public void SwapCamera(CinemachineVirtualCamera _newCam) { if (currentCamera != null) { currentCamera.enabled = false; } currentCamera = _newCam; // Ensure the new camera follows the player if (currentCamera != null) { currentCamera.Follow = PlayerController.Instance.transform; currentCamera.enabled = true; } }Make sure you also check that
PlayerController.Instanceis not null when you try to assign theFollowtarget.- Camera Trigger Logic: In the
CameraTriggerclass, ensure that thenewCamerayou are assigning in the inspector is a validCinemachineVirtualCameraand is set up correctly.
By addressing these points, you should be able to troubleshoot and resolve the issues with the camera not following the player.
October 17, 2025 at 5:23 am #18945October 17, 2025 at 8:06 am #18946::ok so i messed around with some stuff and now the camera is following the player but the camera keeps going down
October 18, 2025 at 2:39 pm #18948::There’s probably some variable in your Cinemachine camera that is not assigned correctly (i.e. camera is following the wrong thing, and the thing is following). You’ll have to tinker around to find that.
Otherwise you can share your project link and I’ll have a look.
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below:
Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.