繁体   English   中英

Unity2d在摧毁游戏对象后试图让相机跟随

[英]Unity2d Trying to make camera follow after destroying gameobject

我试图让我的相机在我的角色被摧毁/重生后跟随它。 有没有办法做到这一点? 我已经附上了我在这里的三个脚本(每个脚本都有它的单独文件);

//Camera Following Script//
using UnityEngine;

public class FollowCam : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;
    [Range(1,10)]
    public float smoothFactor;

    private void FixedUpdate()
    {
        Follow();

    }

    void Follow()
    {
        Vector3 targetPosition = target.position + offset;
        Vector3 smoothPosition = Vector3.Lerp(transform.position, targetPosition,smoothFactor*Time.fixedDeltaTime);
        transform.position = targetPosition;
       
    }
}
//Level Manager//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelManager : MonoBehaviour
{
    public static LevelManager instance;
    
    public Transform respawnPoint;
    public GameObject playerPrefab; 


    private void Awake()
    {
        instance = this;
    }

    public void Respawn()
    {
        Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity);
    }

  
}
//Player Death and Resapwn//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDeath : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Spike"))
        {
            Destroy(gameObject);
            LevelManager.instance.Respawn();
        }

        if (collision.gameObject.CompareTag("Death"))
        {
            Destroy(gameObject);
            LevelManager.instance.Respawn();
        }
    }
}

这也是我得到的错误。 我知道它在问什么,我只是不知道如何解决它。

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at <e414e10bfe5f45729ff122f3359de21b>:0)
FollowCam.Follow () (at Assets/Scripts/FollowCam.cs:18)
FollowCam.FixedUpdate () (at Assets/Scripts/FollowCam.cs:12)

无需破坏您的角色,只需将角色图层移动到相机不可见的图层即可。 这样,当角色预制件看起来像是被破坏了,但您无需实例化所有您需要做的事情就可以访问它,只需更改图层即可。 如果您想更改丢失物品/黄金之类的内容,请将其设置在代码的同一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM