Scripting help?
// Patrol.cs
using UnityEngine;
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.remainingDistance < 0.5f)
GotoNextPoint();
}
}
I would like to have the character to stop at each point for x seconds then move to the next point and do so again for each point. Is there any way i can do this without coroutines? I am not experienced enough to do coroutines so im trying to see if there is any other way.
Answer by oStaiko · Feb 22, 2017 at 06:33 AM
It seems there's been a lot of posts like this where people forgot that the UnityEngine.Time class exists. Not only that, but there's also System.DateTime, and even System.Diagnostics.StopWatch if you're despereate. This is not what what Coroutines were meant for, if you need to count things, just use Time.time. It counts the seconds as a float from the moment the scene (or maybe game?) loaded. Regardless, it can be treated like any old float. By setting a time in the future, like Time.time + 5, we can use an if statement to see when Time.time catches up to that number, which will occur the first frame from 5 seconds after that was set.
// Patrol.cs
using UnityEngine;
using System.Collections;
public class Patrol : MonoBehaviour
{
public Transform[] points;
privatefloat waitTime = 0;
private int destPoint = 0;
private NavMeshAgent agent;
void Start ()
{
agent = GetComponent<NavMeshAgent> ();
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint ()
{
waitTime = 0;
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
void Update ()
{
if (agent.remainingDistance < 0.5f && waitTime == 0)
waitTime = Time.time + 5; //seconds goes here
else if (agent.remainingDistance < 0.5f && Time.time > waitTime)
GotoNextPoint();
}
}
Logic is simple. If we're in range but not timing yet, start timing. Else if were in range and we are past our timer, reset the time and move to new point.
when the enemy gets to each point she starts to sway from left to right then moves to next point. Could it be another script causing this?
It is probably another script yet, This script would not cause them to move during the wait time.
Answer by Dylanpolis · Feb 23, 2017 at 12:35 AM
Thank you for the help! I did not forget by the way. Im just new to unity.
Your answer
Follow this Question
Related Questions
Timer wont work 0 Answers
Getting weird error message!?!?!? 1 Answer
Make enemy patrol without Nav Points? 0 Answers
tagged objects problem 0 Answers
random spawn enemy problem 0 Answers