首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >I漂泊状态似乎不能正常工作

I漂泊状态似乎不能正常工作
EN

Stack Overflow用户
提问于 2022-11-16 10:40:48
回答 1查看 20关注 0票数 0

我一直在尝试建立一个AI系统来跟踪特定的路径点,一旦玩家角色进入触发区域,AI就会开始追逐角色,反之亦然。

这似乎实现了类的工作,但是AI只移动到一个方向点,然后停止,直到玩家进入触发区;之后,如果玩家退出,它再次只会到达一个方向点,然后再次停止。

我有多个路径点,我只想让它一直通过它们,但它只是去其中之一,并停止。

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-17 06:31:37

使用OnTriggerStay而不是OntriggerEnter并尝试

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74459037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档