gpt4 book ai didi

c# - 检测敌人是否面对玩家

转载 作者:行者123 更新时间:2023-11-30 17:44:46 34 4
gpt4 key购买 nike

我正在制作一个游戏,如果距离小于 2 并且敌人面对玩家,文本会出现一个重新启动选项。在更新中有一个 if 和 else 语句,可以检测敌人是在玩家后面还是前面。但是,无论玩家是否面对 npc,一旦距离小于 2,似乎都会调用前面选项。

这个脚本附加到敌人:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class EnemyFollow : MonoBehaviour {

Transform player;
Transform enemy;
public GameObject EnemyCaughtCam;
public Text SheCaughtYou;
public GameObject Restart;

public float Speed = 3f;
public float rotS = 3f;
public float sT = 3f;
NavMeshAgent nav;
float timeTillOptionsMenu = 3.0f;

// Use this for initialization
void Awake() {
player = GameObject.FindGameObjectWithTag ("MainCamera").transform;
//enemy = GameObject.FindGameObjectWithTag ("Enemy").transform;
nav = GetComponent<NavMeshAgent> ();
EnemyCaughtCam.SetActive(false);
SheCaughtYou.text = "";
}

// Update is called once per frame
void Update () {
nav.SetDestination (player.position);
DistanceDeath();

if (npcIsFacingPlayer (player)&& !playerIsFacingNpc(player))
print ("Behind");
else if (npcIsFacingPlayer (player)&& playerIsFacingNpc(player))
print ("In Front");
DistanceDeath ();
}

public void DistanceDeath(){

float distance = Vector3.Distance(player.transform.position,
transform.position);


if (distance < 2 ){

EnemyCaughtCam.SetActive(true);
SheCaughtYou.text = "SHE CAUGHT YOU!";

timeTillOptionsMenu -= Time.deltaTime;
if(timeTillOptionsMenu < 0)
{
Restart.SetActive(true);

}


}

}

public bool npcIsFacingPlayer(Transform other)
{
Vector3 toOther =
other.position - transform.position;
return (Vector3.Dot(toOther, transform.forward) > 0);
}
public bool playerIsFacingNpc(Transform other)
{
Vector3 toOther =
transform.position - other.position;
return (Vector3.Dot(toOther, other.forward) > 0);
}

}

最佳答案

首先,您缺少一些括号,其次有一个 stra DistanceDeath 调用,这是您的函数 Update 的读取方式:

// Update is called once per frame
void Update () {
nav.SetDestination (player.position);

/** what does the call do here? */
DistanceDeath();

if (npcIsFacingPlayer (player)&& !playerIsFacingNpc(player))
print ("Behind");
else if (npcIsFacingPlayer (player)&& playerIsFacingNpc(player))
print ("In Front");

/** are you missing brackets here? Distance Death is always called */
DistanceDeath ();
}

关于c# - 检测敌人是否面对玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29036872/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com