[英]Why my enemy not moving towards target
這也是一個視頻-http://tinypic.com/r/mmagki/9
這是我的start()函數
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
和update()函數
void Update()
{
transform.LookAt(target);
float step = speed * Time.deltaTime;
distance = (transform.position - target.position).magnitude;
//Debug.Log("Now distance -" + distance);
if (distance < 20)
{
// print("In Range");
transform.GetComponent<Animation>().Play("attack", PlayMode.StopAll);
if (isAttacking == false)
{
isAttacking = true;
Hit.playerHealth -= Random.Range(20f, 25f) * Time.deltaTime;
Hit.playerHealth -= Random.Range(20f, 25f);
// StartCoroutine(MyCoroutine(4));
// print("Player Health Status = " + Hit.playerHealth);
if (Hit.playerHealth <= 0)
{
// print("Player dead");
}
}
else
{
}
}
else
{
// print("Out of Range");
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
transform.GetComponent<Animation>().Play("walk", PlayMode.StopAll);
}
}
我的僵屍(敵人)正在接近玩家,當僵屍被撞牆時,他應該上門。
我所做的是,只要Zombie (Set Trigger = checked)
撞到牆,我就將'target'的引用更改為Object with tag 'gate'
。 現在,僵屍沒有朝着大門對象移動(我也設置了標簽“大門”)。 他仍然只朝着玩家前進。 無法更改目標的參考。
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "wall")
{
target = GameObject.FindGameObjectWithTag("gate").transform;
Debug.Log("Yes its a onTrigger Enter function , hitting with wall");
}
}
看來您的大門上沒有對撞機。 將對撞機添加到大門應該可以解決問題。
首先驗證所有標簽的設置正確,然后至少需要在一個對象中添加一個剛體組件,如Unity Docs所說: OnTriggerEnter 。 我建議將其添加到僵屍中,並將UseGravity設置為false。 這樣就可以了...對不起,英語不好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.