简体   繁体   English

如何检查播放器是否已被销毁

[英]how to check if player has been destroyed

in the game I am creating when the player dies I destroy that object and move to a different scene which simply says you died, however the camera follows the player, this creates an error when the player is destroyed as it can no longer follow the player I cannot think of how to scan to see if the player has been destroyed (from inside the cameras script)在我正在创建的游戏中,当玩家死亡时我摧毁了 object 并移动到一个简单地说你死了的场景,但是相机跟随玩家,这会在玩家被摧毁时产生错误,因为它不能再跟随玩家我想不出如何扫描以查看播放器是否已被破坏(从相机脚本内部)

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

public class CameraMotor : MonoBehaviour
{
    public Transform lookAt;
    public float boundX = 0.001f;
    public float boundY = 0.001f;

    private void LateUpdate()
    {
        Vector3 delta = Vector3.zero;

        //to check if inside the bounds on X axis
        float deltaX = lookAt.position.x - transform.position.x;
        if (deltaX > boundX || deltaX < -boundX)
        {
            if (transform.position.x < lookAt.position.x)
            {
                delta.x = deltaX - boundX;
            }
            else
            {
                delta.x = deltaX + boundX;
            }
        }

        //to check if inside the bounds on Y axis
        float deltaY = lookAt.position.y - transform.position.y;
        if (deltaY > boundY || deltaY < -boundY)
        {
            if (transform.position.y < lookAt.position.y)
            {
                delta.y = deltaY - boundY;
            }
            else
            {
                delta.y = deltaY + boundY;
            }
        }

        transform.position += new Vector3(delta.x, delta.y, 0);
    }
}

When the object is destroyed, it's components are destroyed too.当 object 被破坏时,它的组件也被破坏。 So You can simply check if Transform object is null.因此,您可以简单地检查 Transform object 是否为 null。

if (Target == null)
{
   Debug.Log("Object has destroyed");
}

Check if lookAt is not null before looking at the player...在查看播放器之前检查lookAt是否不是null ...

private void LateUpdate()
{
    if(lookAt != null){
        Vector3 delta = Vector3.zero;

        //to check if inside the bounds on X axis
        float deltaX = lookAt.position.x - transform.position.x;
        if (deltaX > boundX || deltaX < -boundX)
        {
            if (transform.position.x < lookAt.position.x)
            {
                delta.x = deltaX - boundX;
            }
            else
            {
                delta.x = deltaX + boundX;
            }
        }

        //to check if inside the bounds on Y axis
        float deltaY = lookAt.position.y - transform.position.y;
        if (deltaY > boundY || deltaY < -boundY)
        {
            if (transform.position.y < lookAt.position.y)
            {
                delta.y = deltaY - boundY;
            }
            else
            {
                delta.y = deltaY + boundY;
            }
        }

        transform.position += new Vector3(delta.x, delta.y, 0);
    }
}

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

相关问题 有没有办法检查游戏对象是否已被销毁? - Is there a way to check if a GameObject has been destroyed? 如何从已被破坏的外部GameObject执行协程 - How to execute Coroutine from external GameObject which has been destroyed MessageWindow-如何知道它是否已被销毁? - MessageWindow - How can I tell if it has been destroyed? Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed 当游戏对象被销毁时出现 MissingReferenceException - MissingReferenceException when gameObject has been destroyed Unity“类型的对象已被破坏”错误 - Unity 'the object of type has been destroyed' error 如何修复 Unity 中的“&#39;GameObject&#39; 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity 销毁预制件中的物体后,如何重新实例化该预制件? - How do I re-instantiate original prefab after an object in that prefab has been destroyed? 如何检查邮件是否已成功发送 - How to check if the mail has been sent successfully
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM