Cinemachine 2D isn't following after respawning the player

833 Views Asked by At

I was trying to make Cinemachine follow my player even after respawn, but it's not working and I honestly don't know what else to do. I don't want to make a new code for camera, because Cinemachine has a lot of good features.

I was thinking that in stead of destroying the object I could reset the variables, so that Cinemachine would still follow the same object, but I'm fairly new to coding, so I don't know how to that.

This is Player code:

using System.Collections;    
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class Death : MonoBehaviour
{
    public static Death death;
    public CinemachineVirtualCamera myCinemachine;

    void Start()
    {    
        if (death == null)    
        {    
            death = GameObject.FindGameObjectWithTag("Death").GetComponent<Death>();
        }

        myCinemachine = GetComponent<CinemachineVirtualCamera>();
    }

    public Transform playerPrefab;    
    public Transform respawn;

    public void RespawnPlayer()
    {
        var spawnedPlayer = Instantiate(playerPrefab, respawn.position, respawn.rotation);
        Debug.Log("TODO Add Spawn Particles");
        myCinemachine.m_Follow = spawnedPlayer;
    }

    public static void KillPlayer (Player player)
    {
        Destroy(player.gameObject);
        death.RespawnPlayer();
    }    
}

This is Death code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Death : MonoBehaviour
{
    public static Death death;

    void Start()
    {
        if (death == null)
        {
            death = GameObject.FindGameObjectWithTag("Death").GetComponent<Death>();
        }

    }

    public Transform playerPrefab;
    public Transform respawn;

    public void RespawnPlayer()
    {
        var spawnedPlayer = Instantiate(playerPrefab, respawn.position, respawn.rotation);
        Debug.Log("TODO Add Spawn Particles");

    }

    public static void KillPlayer (Player player)
    {
        Destroy(player.gameObject);
        death.RespawnPlayer();
    }

}

This picture is a screenshot after respawning. The camera won't move anymore.

0

There are 0 best solutions below