unity3d AI徘徊状态似乎不能正常工作

qoefvg9y  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(114)

我一直在尝试做一个AI系统,它会跟随特定的路点,一旦玩家角色走进触发区,AI就会开始追逐角色,反之亦然。
看起来这确实是一种工作,但人工智能只移动到一个航路点,然后停止,直到玩家走进触发区;之后,如果玩家离开,则其再次仅行进到一个路点,然后再次停止。
我有多个路点,我只是希望它继续循环通过他们,但它只是去其中一个和停止。

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngineInternal;

public class AILocomotion : MonoBehaviour
{
    public Transform playerTransform;
    NavMeshAgent agent;
    Animator animator;
    public float maxTime = 1.0f;
    public float maxDistance = 1.0f;
    float timer = 0.0f;
    bool moveTowards = false;
    bool followWaypoint = false;
    float deaccel= 0.5f;
    float calVelocity = 0.0f;
    public Transform[] points;
    private int destPoint = 0;
    public string animState;

    void Start()
    {

        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
        moveTowards = false;
        followWaypoint = true;  
        
    }

    // Update is called once per frame
    void Update()
    {
        if (moveTowards == true)
        {
            timer -= Time.deltaTime;
            if (timer < 0.0f)
            {
                float sqDistance = (playerTransform.position - agent.destination).sqrMagnitude;
                if (sqDistance > maxDistance * maxDistance)
                {
                    agent.destination = playerTransform.position;
                }
            }
            animator.SetFloat("Speed", agent.velocity.magnitude);
        }
        else if (!moveTowards && agent.velocity.magnitude>0.0f)
        {
            calVelocity = agent.velocity.magnitude;
            calVelocity -= Time.deltaTime * deaccel;
            animator.SetFloat("Speed", calVelocity);
        }

        
        //CHECKS IF BOTH CONDITIONS HAVE MET AND RUNS THE WAYPOINT FOLLOWING CODE
        if (followWaypoint == true && (!agent.pathPending && agent.remainingDistance < 1.0f))
        {
            GotoNextPoint();
        }

        Debug.LogError("Bool" + followWaypoint);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            moveTowards = true;
            //DISABLES THE WAYPOINT FOLLOWING CODE TO RUN THE CHASE CODE INSTEAD
            followWaypoint = false;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            moveTowards = false; 
            //RE-ENABLES THE WAYPOINT FOLLOWING CODE ONCE THE PLAYER LEAVES THE TRIGGER AREA
            followWaypoint = true;
        }
    }

    //THIS IS THE WAYPOINT FOLLOWING CODE
    void GotoNextPoint()
    {
        animator.SetFloat("Speed", agent.velocity.magnitude);
        // 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;

        Debug.LogError("DestPoint = " + destPoint);
        // Choose the next point in the array as the destination.
        // cycling to the start if necessary.
        destPoint = Random.Range(0, points.Length);
    }
}
4c8rllxm

4c8rllxm1#

使用OnTriggerStay而不是OnTriggerEnter并尝试

相关问题