- Home /
Pause for random amount of time at waypoint
Hey guys, I have a script it works as is, but I can't seem to add a random pause at each waypoint. Basically right now he walks to each way point and moves on, I am trying to make him pause at each waypoint, Such as sometimes he will be standing there for 5 seconds, other times he might be standing there for 30 seconds before moving on. Any ideas? A timer of sorts? Also if you are a c# genius, I am trying to make him walk to a random waypoint instead of a set path. : 3
void Patrolling ()
{
// Set an appropriate speed for the NavMeshAgent.
nav.speed = patrolSpeed;
// If near the next waypoint or there is no destination...
if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance)
{
// ... increment the timer.
patrolTimer += Time.deltaTime;
// If the timer exceeds the wait time...
if(patrolTimer >= patrolWaitTime)
{
// ... increment the wayPointIndex.
if(wayPointIndex == patrolWayPoints.Length - 1)
wayPointIndex = 0;
else
wayPointIndex++;
// Reset the timer.
patrolTimer = 0;
}
}
else
// If not near a destination, reset the timer.
patrolTimer = 0;
// Set the destination to the patrolWayPoint.
nav.destination = patrolWayPoints[wayPointIndex].position;
}
i think rendom.range will be perfect for your problem like
int i= Rendom.Ranger(4,31) \\it will give rendom value from 4-30
patrolTimer=i;
Thanks man, very interesting thing to know. You up for showing me how it should look in the script? Before things get messy haha
should this be in the update? or in the void patrolling?
int i = Random.Range (4, 31);
patrolWaitTime = Random.range(4,31);
and put it some where so it can be updated
or
int i= Random.range(4,31);
patrolWaitTime = i; //almost same thing
may be in above script so that every-time when gameobject goes from current to next waypoint it will have a new random value .
Answer by chadli · Feb 18, 2015 at 05:21 AM
There really are a million different ways to do it.
Look into the Random.Range documentation to see how to implement a number generator. See the link below.
http://docs.unity3d.com/ScriptReference/Random.Range.html
To pause you could use something like Random.Range(0, 10) and have the object wait through that range.
As far as the Waypoints go, try and play around with creating your own algorithm or look up anothers.
You could just have it visit adjacent nodes base on whether the random range returns a negative or positive and alternate between going up and down or side to side. It is really up to you.
Or you could do a range from 0-5 and have it go towards the index it needs to go. Does that make sense? In my example below O is the location of the object, and G is the goal of where you want to go. 0-5 would represent the spaces (1) that it can travel.
[1,O,1]
[1,1,1]
[1,G,1]
I understand what you mean with the example you gave. I'm not exactly sure how to even begin to set that up tho haha. Bit of a noob
Your answer
Follow this Question
Related Questions
Adding a timer to spawner 2 Answers
Pause at end of game 2 Answers
How to add waypoints/ timers for a car game! 1 Answer
Random spawning in open environment 1 Answer