我如何阻止我的AI巡逻和追逐玩家然后回到Unity巡逻?

时间:2016-08-26 04:08:58

标签: c# unity5

根据标题,我已经制作并修改了统一巡逻AI脚本,并添加了追逐脚本,但现在当AI看到播放器时,它不会停止巡逻并继续巡逻但只是盯着玩家而不是追逐玩家。

代码如下:

using UnityEngine;
using System.Collections;

public class CHASE : MonoBehaviour 
{

    public Transform player;
    public Transform myTransform;
    static Animator anim;
    public float moveSpeed = 6.0f;
    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    void Start () 
    {
        anim = GetComponent<Animator> ();
        agent = GetComponent<NavMeshAgent>();
        agent = GetComponent<NavMeshAgent>();
        GotoNextPoint();
    }

    void Update () 
    {
        Vector3 direction = player.position - this.transform.position;
        direction.y = 0;
        float angle = Vector3.Angle (direction, this.transform.forward);
        if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) 
        {
            myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                Quaternion.LookRotation (direction), 0.1f * Time.deltaTime * 50.0f) ;

            anim.SetBool ("isIdle", false);
            if (direction.magnitude > 5) 
            {
                this.transform.Translate (0, 0, 0.05f);
                anim.SetBool ("isChasing", true);
                anim.SetBool ("isDeath", false);
            }
            else
            {
                anim.SetBool ("isChasing", false);
                anim.SetBool ("isDeath", false);
            }
        }
        else if(agent.remainingDistance < 0.5f)
        {
            anim.SetBool("isIdle", true);
            anim.SetBool("isChasing", false);
            anim.SetBool("isDeath", false);
            GotoNextPoint();
        }
    }

    void GotoNextPoint() 
    {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }
}

0 个答案:

没有答案