[英]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.