敌人到达目标时禁用另一个脚本?

时间:2018-04-17 16:40:33

标签: c# unity3d

如果我的敌人到达目标玩家后如何禁用它?我想在我的代码中在我的GameManager下引用这个名为Casting的脚本。当我禁用它时,即使敌人正好在目标玩家之上,我的玩家仍然可以施放。是否存在阻止这种情况的功能?

public class aiEnemy : MonoBehaviour, IPooledObject {

public float lookRadius = 40f;

public Casting stop;

Transform target;
UnityEngine.AI.NavMeshAgent agent;

Rigidbody theRigidBody;

void Start(){
    target = PlayerManager.instance.player.transform;
    agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}


void Update(){
    float distance = Vector3.Distance (target.position, transform.position);
    if (distance <= lookRadius)
    {
        agent.SetDestination (target.position);

        if (distance <= agent.stoppingDistance)
        {
            FaceTarget ();
        }

        if (distance < 1f) // or some distance
        {
            stop.enabled = false;
        }
    }

}

void FaceTarget()
{
    Vector3 direction = (target.position - transform.position).normalized;
    Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
    transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}


// Use this for initialization
public void OnObjectSpawn () {


    //myRender = GetComponent<Renderer> ();
    theRigidBody = GetComponent<Rigidbody>();



}

void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere (transform.position, lookRadius);
}

}

0 个答案:

没有答案