- Home /
how to create waypoints for a car to follow?
so i am making a racing game, but i cant seem to create a waypoint for the AI Cars. i have itween visual editor but since i am a noob at this i cant seem to find a waypoint script in the files. is there a script in there i am just too blind to see, or is there a script i have to create and put in?
Answer by cdrandin · Jan 24, 2013 at 02:03 AM
I took this from Unity's FPS tutorial. Even though it is for a FPS, it isn't too difficult to transfer the idea to your unique game. http://download.unity3d.com/support/old-resources/files/FPS_Tutorial_3.pdf First section is "Waypoints", you will have to download the FPS_Complete and look up the script to see it.
Its not a fail if it doesnt explain it to a total noob like me
please provide me pdf link as above link is not working..:-(
Answer by sam32x · Jan 25, 2013 at 02:29 AM
create a few empty gameobjects and label them waypoint1, waypoint2 etc. then make another empty gameobject and put a collider on it and check the trigger box, then put this script on the trigger
var waypoint1 : Transform;
var waypoint2 : Transform;
var waypoint3 : Transform;
//extend this if you want
var waypoint = 1;
function Start () {
transform.Position = waypoint1.position;
}
function Update () {
// extend this part too if you extended the top
if(waypoint == 2){
transform.Position = waypoint2.position;
}
if(waypoint == 3){
transform.Position = waypoint3.position;
}
}
//im assuming your car has a collider
function OnTriggerEnter () {
waypoint += 1;
}
then put this on the AI car
var trigger : Transform;
//make sure you assign the objects to the script in the editor
function Update () {
transform.LookAt(trigger);
}
note that this wont look very realistic because the car will turn instantly. you could try RotateTowards but i didnt use it because i have never been able to get it to work
Writing conditional code with so many if-statements is a horrible practice.
This post comes up as one of the first when googling, so it WILL be getting a lot of hits.
Your answer
Follow this Question
Related Questions
Random Running and Follow at Certain Distance 0 Answers
I need to instantiate a empty gameObject 2 Units right/left of a 'Enemies' Transform? 3 Answers
Incredibly high speed tweening/enemy movement 0 Answers
AI Pathfinding 1 Answer
How to make enemy attack multiple unit rather then single?(updated) 1 Answer