3
我正在製作一個遊戲,如果距離小於2且敵人面對玩家,則文本會顯示重新啓動選項。在更新中有一個if和else語句,它應該檢測敵人是在玩家後面還是在玩家面前。然而,當距離小於2時,前面的選項似乎被調用,無論球員是否面對NPC。檢測敵人是否面臨玩家
該腳本連接到敵人:
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);
}
}
謝謝,但不幸的是,當我加入他們仍然稱爲距離死亡函數,我認爲這可能是與語句本身的邏輯有關,但無法弄清楚 – Cdalyz 2015-03-13 16:24:23
@Calyalyz,還有另一個'DistanceDeath'我剛剛看到,se cond行 – martin 2015-03-17 09:28:05
這工作,刪除了第二個DistanceDeath(),感謝一百萬 – Cdalyz 2015-03-18 21:23:40