Rotation of the object at the end of the path

Hello everyone. It’s me again. I need the fighters in the squad to turn in the right direction when they stop.
I found some code in the documentation.

        IEnumerator start()
        {
            ai.destination = target2;
            // Start to search for a path to the destination immediately
            ai.SearchPath();
            // Wait until the agent has reached the destination
            while (!ai.reachedDestination)
            {
                yield return null;
            }
            // The agent has reached the destination now
            print("end");
        }

But in this code there are coroutines. I have not yet learned how to implement them. I read the documentation about coroutines.
Here’s the code I’m using.

using UnityEngine;
using System.Collections;

namespace Pathfinding
{
    /// <summary>
    /// Sets the destination of an AI to the position of a specified object.
    /// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
    /// This component will then make the AI move towards the <see cref="target"/> set on this component.
    ///
    /// See: <see cref="Pathfinding.IAstarAI.destination"/>
    ///
    /// [Open online documentation to see images]
    /// </summary>
    [UniqueComponent(tag = "ai.destination")]
    [HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
    public class aid : VersionedMonoBehaviour
    {
        /// <summary>The object that the AI should move to</summary>
       
        public Vector3 target2 = new Vector3();
        
       
        public bool prom = false;
        IAstarAI ai;

       
        void OnEnable()
        {
           // print(target.position);
            
            ai = GetComponent<IAstarAI>();
            // Update the destination right before searching for a path as well.
            // This is enough in theory, but this script will also update the destination every
            // frame as the destination is used for debugging and may be used for other things by other
            // scripts as well. So it makes sense that it is up to date every frame.
            if (ai != null) ai.onSearchPath += Update;

           

        }

        void OnDisable()
        {
            if (ai != null) ai.onSearchPath -= Update;
          
        }

        /// <summary>Updates the AI's destination every frame</summary>
        void Update()
        {

           
            if (prom==true)
            {
               
                if (target2 != null && ai != null)
                {
                    ai.destination = target2;

                   
                }
                prom = false;             
            }

            
        }

    }
}

This is a reworked script from the example. Please help me later. I solved the last problem on my own for almost a month.
How do I add my script?

Hi

If you just want something very simple, then you can do something like:

void Update () {
    if (ai.reachedDestination) {
        // Rotate agent by a small amount towards the desired direction here
    }
}

I also tried a little differently. In the script “AIPath” in line 271

public virtual void OnTargetReached () {
print(“end”);
}

. The result is the same. After a certain period of time, the script is updated and is triggered again and again. I understand that this is how the library works. But I can’t think of a way to make the script work once.

I was able to figure out how to stop the update.

void Update()
        {                              

            if (ai.reachedDestination )
            {
                ai.canSearch = false;
                ai.SetPath(null);
                print("end");

             }           
        }
1 Like