1

我正在开发一个 2D 平台游戏,我正在使用 Cinemachine 跟随我的玩家。
当玩家在 -20 y 以下的平台下落时,玩家被摧毁并实例化为预期的生成点的克隆,但相机没有跟随,因为原始玩家被摧毁:它在“跟随”上说失踪插槽
有什么办法可以解决吗?我更喜欢使用 Destroy 和 Instantiate 作为重生,而不是将原始玩家传送到重生点。

这是重生脚本(GameMaster)

public class GameMaster : MonoBehaviour
{

    public static GameMaster gm;



    void Start()
    {
        if (gm == null)
        {
            gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
        }
    }
    public Transform playerPrefab, spawnPoint;
    public int spawnDelay = 2;

    public void RespawnPlayer() {
        //yield return new WaitForSeconds(spawnDelay);

        Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
        Debug.Log("ADD SPAWN PARITCAL");
    }

    public static void Killplayer(Player player) {
        Destroy(player.gameObject);
        gm.RespawnPlayer();
    }
}

如果需要,这是播放器脚本

public class Player : MonoBehaviour
{
    [System.Serializable]
    public class PlayerStats
    {
        public int Health = 100;

    }
    public PlayerStats playerStats = new PlayerStats();
    public int FallBoundary = -20;
    void FixedUpdate()
    {
        if (transform.position.y <= FallBoundary)
        {
            DamagePlayer(1000);
        }
    }

    public void DamagePlayer(int damage) {
        playerStats.Health -= damage;
        if (playerStats.Health<=0)
        {
            Debug.Log("Kill Player");
            GameMaster.Killplayer(this);
        }
    }
}
4

2 回答 2

2

您可以在 start 方法中缓存 Cinemachine,然后指定在重生时跟随玩家。

您的代码将变为

using Cinemachine;

public class GameMaster : MonoBehaviour
{
    public static GameMaster gm;
    public CinemachineVirtualCamera myCinemachine;


    void Start()
    {
        if (gm == null)
        {
            gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
        }

        myCinemachine = GetComponent<CinemachineVirtualCamera>();
    }
    public Transform playerPrefab, spawnPoint;
    public int spawnDelay = 2;

    public void RespawnPlayer() {
        //yield return new WaitForSeconds(spawnDelay);

        var newPlayer = Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
        Debug.Log("ADD SPAWN PARITCAL");

        myCinemachine.m_Follow = newPlayer;
    }

    public static void Killplayer(Player player) {
        Destroy(player.gameObject);
        gm.RespawnPlayer();
    }
}
于 2019-05-29T07:47:20.720 回答
0

您必须像这样分配新对象:

myCinemachine.m_Follow = spawnedPlayer;
于 2019-05-28T06:32:28.647 回答