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

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #18924
    Tyler Fruge
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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
    #18925
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your CameraManager and CameraTrigger scripts?

    #18930
    Tyler Fruge
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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);
            }
        }
    }
    #18932
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @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);
            }
        }
    }
    #18933
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Looking at your code, there are a couple of potential issues that might be preventing the camera from properly following the player:

    1. Camera Initialization: In the CameraManager class, the currentCamera is set in the Awake method based on which camera is enabled. If none of the cameras in allVirtualCameras are enabled at the start (or if PlayerController.Instance is not set properly), currentCamera may remain null, which could lead to issues when trying to swap cameras or setting the Follow target.

    2. Camera Follow Target: In the Start method, you set the Follow property of all virtual cameras to the player. However, if SwapCamera is called and the new camera (newCamera) does not have its Follow target set to the player, it won’t follow the player. You might want to ensure that newCamera.Follow is also assigned to PlayerController.Instance.transform when you swap cameras.

    3. Camera Activation: In the SwapCamera method, you disable the current camera without checking if it is null. If currentCamera is null, trying to access enabled will throw a null reference exception. You should add a check to ensure currentCamera is not null before trying to disable it.

    4. 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 SwapCamera method 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.Instance is not null when you try to assign the Follow target.

    1. Camera Trigger Logic: In the CameraTrigger class, ensure that the newCamera you are assigning in the inspector is a valid CinemachineVirtualCamera and 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.

    #18945
    Tyler Fruge
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    ok so what’s the issue? i don’t really trust ai assistants.

    #18946
    Tyler Fruge
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    ok so i messed around with some stuff and now the camera is following the player but the camera keeps going down

    #18948
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: