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?