1

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.

  • do you do anything else to the camera on death for example? – BugFinder Dec 30 '19 at 22:25
  • I do. I tried making it to follow the clone, but it's not working and I don't know what else to do – Patrick Sramka Dec 30 '19 at 22:41
  • well whatever you changed on death you need to undo.. – BugFinder Dec 30 '19 at 22:43
  • Done. I deleted everything connected to the Cinemachine in Death code – Patrick Sramka Dec 30 '19 at 22:45
  • What should I do next? – Patrick Sramka Dec 30 '19 at 22:51
  • well if you arent changing anything to do with the camera, then it should still be following your character – BugFinder Dec 30 '19 at 23:21
  • It's not. It was like that without all those line related to Cinemachine before and it didn' work. That's why I added them. – Patrick Sramka Dec 30 '19 at 23:26
  • so what else changes? something somewhere is stopping it, narrow it down by removing code and slowly readding it – BugFinder Dec 30 '19 at 23:28
  • I would say it's because of this ```myCinemachine.m_Follow = spawnedPlayer;``` The "spawned part should be changed I think, but I don't know for what to change it. – Patrick Sramka Dec 30 '19 at 23:37
  • 1
    I do not see where you initially assign the camera other than when you try and respawn. Also do not see where you call KillPlayer. You `Destroy(player.gameObject);` but I do not see you assign anything else to your player object. I am guessing that somewhere you are following the `Player`. Since it works up until you KillPlayer and respawn. I do not think there is enough here to correctly fix the issue as some pieces of code are missing. - Also Player Code and Death code look the same just about....... – Sorceri Dec 30 '19 at 23:59
  • are you sure you arent getting any errors like trying to destroy a destroyed object? or anything.. we cant recreate from what you've given us – BugFinder Dec 31 '19 at 00:48

0 Answers0