我遇到的问题是,当敌人进入射击范围时,即使玩家离开射程,他也会停止并且永远不会再跟随玩家。为了检测玩家是否进入了射击场,我为敌人制造了一个球体对撞机。
using UnityEngine;
using System.Collections;
public class EnemyWithRifleMovement : MonoBehaviour {
private GameObject player;
private NavMeshAgent nav;
private bool playerInRange = false;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
player = GameObject.FindGameObjectWithTag("Player").gameObject;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if(playerInRange)
{
nav.SetDestination(transform.position);
}
else if(playerInRange == false)
nav.SetDestination(player.transform.position);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = true;
}
}
void OnTriggetExit(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = false;
}
}
}
答案 0 :(得分:0)
你的代码看起来很好,除了有一个拼写错误。将OnTriggerExit
替换为playerInRange = (Vector3.Distance(transform.position, player) <= range
,它应该可以使用!
此外,在我看来,通过.checkbox-inline > .help-button {
padding-left: 100px;
}
更容易获得敌人和玩家之间的距离。