【问题标题】:AI path changing when OnTriggerEnter with UNITY使用 UNITY 的 OnTriggerEnter 时 AI 路径发生变化
【发布时间】:2021-12-25 01:58:26
【问题描述】:

在这里我分享我的 AiPathfinding 脚本。我希望我的 AI 在游戏开始时走到目的地(目标)。但是,当 AI 碰撞某个对象(使用 tag="bullet" 查找)时,我想设置新的目的地。虽然代码没有报错,但当玩 AI 时,它会先到达第一个目的地,然后在途中停止,即使它在途中与某个物体发生碰撞。

有人可以看看吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;

    public class AIPathfinding : MonoBehaviour
    {
        [SerializeField] public Transform target;
        [SerializeField] public Transform target2;
        [SerializeField] public float speed = 200f;
        [SerializeField] public float nextWayPointDistance = 3f;
    
        [SerializeField] Path path;
        [SerializeField] int currentWaypoint = 0;
        [SerializeField] bool reachedEndOfPath = false;
    
        [SerializeField] Seeker seeker;
        [SerializeField] Rigidbody2D rb;
    
        // Start is called before the first frame update
        void Start()
        {
            seeker = GetComponent<Seeker>();
            rb = GetComponent<Rigidbody2D>();
            InvokeRepeating("UpdatePath", 0f, 0.5f);
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.gameObject.tag == "bullet")
            {
                UpdatePath(target2);
            }
        }
    
        public void UpdatePath(Transform target2)
        {
            if (seeker.IsDone())
            {
                seeker.StartPath(rb.position, target.position, OnPathComplete);
            }
        }
    
        void OnPathComplete(Path p)
        {
            if (!p.error)
            {
                path = p;
                currentWaypoint = 0;
            }
        }
    
        void Update()
        {
            if (path == null)
            {
                return;
            }

            if (currentWaypoint >= path.vectorPath.Count)
            {
                reachedEndOfPath = true;
                return;
            }
            else
            {
                reachedEndOfPath = false;
            }
    
            Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
            Vector2 force = direction * speed * Time.deltaTime;
    
            rb.AddForce(force);
    
            float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
    
            if (distance < nextWayPointDistance)
            {
                currentWaypoint++;
            }
        }
    }

【问题讨论】:

    标签: unity3d artificial-intelligence path-finding


    【解决方案1】:

    我的猜测是你从未停止过 Coroutine InvokeRepeating("UpdatePath", 0f, 0.5f)。所以在子弹击中时调用 CancelInvoke() 并更改搜索器的目标(再次启动 InvokeRepeating(...))。

    我假设 NavMeshAgent.SetDestination(Vector3 position) 在函数 seeker.SetTarget() 中被调用。并以某种方式计算和设置路径。

    您也可以将 Update Path 的主体移动到 Update 函数中或从那里调用它。没关系间隔。或者在您的自定义间隔中调用它。只需计算下一次要调用它的时间,如果 nextUpdatePathTime(Time.time 加上您的冷却时间)小于 Time.time,则调用它并计算下一个 nextUpdatePathTime。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2018-07-02
      • 1970-01-01
      • 2015-09-21
      相关资源
      最近更新 更多