- Home /
How can I make an item choose between several different waypoints randomly and then make it execute something when it arrives to the waypoint?
The purpose of the game is to capture a golden ball which moves to different positions in the scene. I want to make about 8 waypoints over the scene which the ball can randomly choose one(with exception from the one it is placed on at that moment) and then go to a second waypoint, in 1 second lower down, wait there for 10 seconds and then take 1 sec to get up over the map and do the thing all over again. Preferably if it could take 3 seconds to move to next waypoint too so the whole operation takes 15 seconds. I have searched all day for an answer to this but cant find anything. Any explanation or link to something helpful would be great!
Answer by Statement · Mar 08, 2013 at 02:55 PM
Random.Range to get a random number to select one of the waypoints (if it gives the same waypoint that you currently are at, you could just try again).
WaitForSeconds to wait for some time in the coroutine.
Vector3.MoveTowards or Vector3.Lerp to move the position in conjunction with transform.position.
Read the docs for each link provided and you should be able to puzzle it together. If you still are stuck, but have read the docs, please explain what confuses you and I'll try to help a bit further.
Answer by poncho · Mar 08, 2013 at 02:51 PM
enumerate the waypoints, then use a random like Random.Range(0,8), get your random waypoint index, use that waypoint, getyourself a time dependant variable, float myTimeDependantVariable, that gets the time from the update Time.deltaTime, add states, countingto1, countingto10, countingto3, change states according to what you need. hope you get it, happy coding
Answer by Jerkenhag · Mar 08, 2013 at 06:52 PM
Thanks for the answers! I have read through the documents and searched at some other places to find out how to lower the object for 10 seconds. However I cant seem to make the object move to next waypoint with this code but when I have the object not lowered it manages to move back and forth between the waypoints but not with the time of 3 seconds it just pops between the points. This is not the major concern so if I just could get some tip about how to make this code go to start again that would probably be just fine!
var waypoint : int;
var currentWaypoint : int;
var start : Transform;
var end;
waypoint = 1;
currentWaypoint = 1;
var waypoint1 : Transform;
var waypoint2 : Transform;
var waypoint3 : Transform;
var waypoint4 : Transform;
function Start(){
currentWaypoint = Random.Range(1, 4);
if(currentWaypoint != waypoint){
if(currentWaypoint == 1){
transform.position = Vector3.Lerp(start.position, waypoint1.position, 3);
yield WaitForSeconds(1);
transform.Translate(Vector3.up * -10, Space.Self);
yield WaitForSeconds(10);
transform.Translate(Vector3.up * 10, Space.Self);
yield WaitForSeconds(1);
waypoint = currentWaypoint;
}
else if(currentWaypoint == 2){
transform.position = Vector3.Lerp(start.position, waypoint2.position, 3);
yield WaitForSeconds(1);
transform.Translate(Vector3.up * -10, Space.Self);
yield WaitForSeconds(10);
transform.Translate(Vector3.up * 10, Space.Self);
yield WaitForSeconds(1);
waypoint = currentWaypoint;
}
else if(currentWaypoint == 3){
transform.position = Vector3.Lerp(start.position, waypoint3.position, 3);
yield WaitForSeconds(1);
transform.Translate(Vector3.up * -10, Space.Self);
yield WaitForSeconds(10);
transform.Translate(Vector3.up * 10, Space.Self);
yield WaitForSeconds(1);
waypoint = currentWaypoint;
}
else if(currentWaypoint == 4){
transform.position = Vector3.Lerp(start.position, waypoint4.position, 3);
yield WaitForSeconds(1);
transform.Translate(Vector3.up * -10, Space.Self);
yield WaitForSeconds(10);
transform.Translate(Vector3.up * 10, Space.Self);
yield WaitForSeconds(1);
waypoint = currentWaypoint;
}
}
else if(currentWaypoint == waypoint){
currentWaypoint = Random.Range(1, 4);
}
}
I made it work by adding:
while(currentWaypoint<5){
"code"
}
so that it would go on forever. Thank you for your tips!
Your answer
Follow this Question
Related Questions
Random Running and Follow at Certain Distance 0 Answers
How can I make AI follow waypoints and choose random ones? 1 Answer
AI Pathfinding 1 Answer
Wandering enemies 1 Answer
Confused about copying Lists! 1 Answer