[英]how to check if player has been destroyed
在我正在創建的游戲中,當玩家死亡時我摧毀了 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);
}
}
當 object 被破壞時,它的組件也被破壞。 因此,您可以簡單地檢查 Transform object 是否為 null。
if (Target == null)
{
Debug.Log("Object has destroyed");
}
在查看播放器之前檢查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.