Home > Software design >  AI path changing when OnTriggerEnter with UNITY
AI path changing when OnTriggerEnter with UNITY

Time:12-26

Here I share my AiPathfinding script. I want my AI walks to a destination (target) when game starts. however, when AI collide an certain object (finding with tag="bullet") then I want to set new destination. Although code does not give error, when playing AI goes to first destination then stops there even though it collides with certain object on the way.

Can someone have a look please?

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  ;
            }
        }
    }

CodePudding user response:

My guess is that you never stopped the Coroutine InvokeRepeating("UpdatePath", 0f, 0.5f). So do call CancelInvoke() when the bullet hits and change the target of the seeker (Start InvokeRepeating(...) again).

I assume NavMeshAgent.SetDestination(Vector3 position) is called in the function seeker.SetTarget(). And somehow Path gets calculated and set.

You could also just move the body of Update Path into the Update function or call it from there. Nevermind the Interval. Or do call it in your custom interval. Just calculate the next time you want to call it so if the nextUpdatePathTime (Time.time plus your cooldown) is smaller than Time.time then call it and calculate the next nextUpdatePathTime.

  • Related