Ok so im a 3d artist and i im trying to make IA for NPC to walk around…i have basic code from unity documentation and it is like this now :
// Patrol.cs
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Patrol : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint() {
// 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;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
now i know from the documentation that this code(is to wait) :
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour
{
void Start()
{
StartCoroutine(Example());
}
IEnumerator Example()
{
print(Time.time);
yield return new WaitForSecondsRealtime(5);
print(Time.time);
}
}
my question is where to i add to the one above as just one script and make it work???can any one help me ? thanks a lot in advance
Supposing you want your agent to wait when he has reached a point:
public class Patrol : MonoBehaviour
{
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
private bool waiting;
private WaitForSecondsRealtime wait;
void Start ()
{
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
wait = new WaitForSecondsRealtime(5);
StartCoroutine( GotoNextPoint() );
}
IEnumerator GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
yield break;
waiting = true ;
yield return wait;
waiting = false ;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update ()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f && !waiting)
StartCoroutine( GotoNextPoint() );
}
}
@Hellium please help me when you have 5m to spare…and thanks again for the first tip and help on the code,
Thanks for the replay… The code i have now is this :
// Patrol.cs
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Patrol : MonoBehaviour
{
public Transform points;
private int destPoint = 0;
private NavMeshAgent agent;
private bool waiting;
private WaitForSecondsRealtime wait = new WaitForSecondsRealtime(5);
void Start ()
{
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
StartCoroutine( GotoNextPoint() );
}
IEnumerator GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
waiting = true ;
yield return waiting;
waiting = false ;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update ()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f && !waiting)
GotoNextPoint();
}
}
What i want is for my npc to go to point A,then B etc …but i want him to wait for a couple of seconds and then move to point B…etc…the code above is the one that is not working…The one that works is :
public class Patrol : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint() {
// 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;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
but that one the npc does not stop…just keeps moving from poitn a to b and to c etc…( this code above is the one from Unity - Manual: Making an Agent Patrol Between a Set of Points
and i want to add the waiting part of it as well…hope you understand what i said…thanks a lot again