- Home /
AI Car Waypoints - Odd Behaviour...
Hi there,
I am trying to set up a basic system in which my AI cars drive around the track, by way of waypoints. I thiought this would be simple, but the code that I have isn't working... each time the car hits a waypoint, it slows down and moves to the next. It only gets to a couple of waypoints before coming to a complete stop... any ideas?!?!?!
for (var i = 0; i < Waypoints.length - 1; ++i)
{
transform.position = Vector3.MoveTowards(transform.position,Waypoints[i].transform.position,(fSpeed * Time.deltaTime));
}
Actually... looking at it closely, it's only moving to the second waypoint... and it slows down.... REALLY weird...
Silly me, it's counting through the index every frame, but it takes time to perform the transform.Translate. DUH. I just need to make it wait until a waypoint is hit before it moves to the next. I'll be up all night, I know it... DA$$anonymous$$N $$anonymous$$Y NOOBNESS!!!!!
It also helps if you turn on the box colliders. DOH. FIXED!!!!!
Answer by POLYGAMe · Feb 15, 2012 at 07:20 AM
For anyone else interested in this, here is what I did:
I got rid of the for loop and put an OnTriggerEnter function that when triggered (by car hitting waypoint), increments the waypoint array index. This way, every time a waypoint is hit, the car moves on to the next :)
It's really basic and needs a LOT of waypoints to be reasonably smooth but for a simple game like mine, it's fine.
Hope this helps someone else!!!
Can the triggers overlap? what if gets stuck inside a trigger. What about steep corners < 90 degrees? Is this method better than checking distance between player and waypoint and changing the waypoint to the next one and keep doing the same over and over again?
Haha, nah... found it had a load of bugs (unless you have VERY basic tracks). We went with the distance method ;-)
Basically we ended up with WAY less waypoints and lerped between them. $$anonymous$$uch smoother.
Your answer
Follow this Question
Related Questions
How to make an AI of a car to jump if there is any obstacle 2 Answers
Enemy AI things to consider 2 Answers
Help With Waypoints script 1 Answer
Smooth turn.... 1 Answer