- Home /
City traffic Lanes
It's been a while, but I've returned to working on an open city world in unity. I know how to script a car, and have it follow waypoints and so on, what I'm struggling with is how to have the cars in lanes, so opposing sides of the road move in opposite directions.
I know you could have 'sets' of waypoints, but then, how would you make sure the cars moved in random directions? So when they get to junctions, how do they decide whether they are going to go left, or right, or straight on?
My gripe is that games over the years have made it look so simple; from the obvious like Grand Theft Auto, to just being part of the race in games like Need For Speed. Is there just a set of formulae or something that people have used? Who knows.
So yeah, any assistance would be great.
[EDIT] Having figured most of the problem is about the multiple choice decisions the cars need to make at each junction, this is the main problem. Having the cars make different choices already solves the 2-way road problems, so I just need to grasp how to get each car to make a decision and then take the appropriate set of waypoints.
As to your last point, I am pretty sure that a professional game company will not use Unity for their production games. The developers and designers at Grand Theft Auto and Need for Speed have full time dedicated programmers that work for years to achieve the final effects. It is likely that what you see as "looks so simple" is huge amounts of time and research into what is actually "looks so realistic". Under the covers, it may be far from simple.
No- I never said they use Unity. GTA used RenderWare, and then RAGE for IV. I was not trying to imply that. But to the other half of your comment, yes, that could be true.
Answer by Tasarran · May 31, 2012 at 08:53 PM
Here's a suggestion... This won't solve the problem, but it might help to get outside the box...
Use one set of waypoints, running down the middle of the street.
But instead of having your cars try to line up the center of the car with the waypoint path, have the car line up with a point set just outside the driver's side door.
That way, cars can use either set, and the side of the road they are on will be defined by the way they are going.
Would only work for two lane roads, but I could see the use of a Lane variable that would move the car farther and farther away from the middle...
Answer by hathol · May 28, 2012 at 01:26 AM
Car AI is quite a challenging thing to do (as is any form of AI actually). The easiest solution that I can think of, is to just check which side of the road your car is on and adjust the steering behaviour accordingly. For checking, you can do something like
Vector3 v_direction = v_currentWaypoint - v_lastWaypoint;
// assume waypoints are Vector3s and set in the middle of the road
Vector3 v_right = Vector3.Cross(v_direction, Vector3.up);
if(Vector3.Dot(transform.position - v_lastWaypoint, v_right) > 0)
// you're on the right side
else
// you're on the left side
Alternatively, you could define your navmesh(es) by using one-way waypoints only. So you basically have a set of waypoints for each lane and only allow traversing them in one direction (this can usually be achieved by just not setting the previous waypoint as a neighbour of the following one.
For decisions on crossroads: I would just randomly select any waypoint on the navmesh and let the pathfinding do the rest. If the car reaches the waypoint, just pick another one. Or you could just scrap pathfinding altogether for that and random pick one of the neighbouring waypoints.
Quick Edit: This will only take care of your very basic navigation needs. You will still have to deal with stuff like avoiding obstacles, trafic signals and so on
Voting for informity. I can do traffic lights and obsatcle avoiding pretty easily. The changing the steering for each side of the road doesn't sound such a good idea, as each car would keep seem to try to steer into the middle of the road before correcting itself, so that wouldn't look great. The alternative idea could be good. It's not really the sides of roads that's a problem, I guess. because by choosing random paths at junctions, the traffic will already look like it's driving in multiple lanes due to the choices they make. So there's an update to the question.
Your answer
Follow this Question
Related Questions
How can I change the "forward" direction of my model? 5 Answers
Attempting to add wheel friction to a 2d car 1 Answer
Pointing towards Waypoint 2 Answers
Unity Car tutorial, Wrong way code 0 Answers