- Home /
Spawning AI cars - weighted to ahead?
EDIT: Line breaks aren't working?
I'm working on a little project where I drive around a small city with a top-down but slightly angled camera view (like [GTA Chinatown Wars][1]). Cars are instantiated from spawners that aren't in camera view, then destroy if they drive too far from the player.
To spawn a new car, I loop through all my spawner gameobjects and if they are within a certain distance to the player (and not in view of the camera), then they get added to a 'spawner' list, of which I pick a random.
The loop is something like this;
float dist = Vector3.Distance(player.transform.position, spawner.transform.position);
Vector3 screenPos = Camera.main.WorldToViewportPoint(spawner.transform.position);
bool inView = screenPos.z > 0 && screenPos.x > 0 && screenPos.x < 1 && screenPos.y > 0 && screenPos.y < 1;
if (!inView && dist < 15) { list.Add(spawner); }
This works, but I find that when I'm driving around barely any traffic spawns on roads ahead of me, maybe because the camera angle is removing a chunk of the possibility area.
Is there anyway to 'weight' it so spawners in the direction the camera (or beyond) have more chance of being picked, rather than ones behind me? In games like GTA Chinatown Wars you will be driving through rows of traffic, and I'm trying to get more density.