CarAI - project a path in front of vehicle that ends at node tagged "intersection"

Hello,

I have enemy AI cars chasing the player car on a system of roads using waypoints/point graph. It’s working really well and I started implementing a vision system for the enemy cars.
Basically, the idea is that when the enemy car looses sight of the player it should still continue driving (assuming, like a human would do, that the player is still driving further down the road) until it reaches an intersection.
If the enemy car reaches an intersection and still doesn’t “see” the player car, then the chase is over, and it drives to another random intersection - and so forth.

Basically, I would need to switch the target position from player car to the closest intersection in the direction the enemy car is driving. Finding the closest object tagged “intersection” would work but I can’t figure out how to make sure it’s in the driving direction and on the same road.

Is there a way to “project” a path in front of the enemy car until it reaches an intersection node and make it the target?
Or some other way of achieving this?

Thank you

Ondras

Well, the simplest way I could think of would be to make two new AI states~
1: chasing, (Bool)
1: CanSee, (Bool)

And a few checks
Sudo code
`

public vector3 lastSeenHere
public Transform Target
public bool CanSee

public void CheckIfYouCanSee(){
If(Raycast at Target != true){
CanSee = false
}
else{
CanSee = true
lastSeenHere == Target.position;
}
}

public void Chase(){
if(CanSee == true){
MoveAt(Target);
}
if(CanSee != true){
MoveAt(lastSeenHere);
}
}

public void Move(Transform Target){
if(this.position == Target && CanSee != true){
chasing == false;
// If chasing == false the car will exit at the next intersection~
}
else{
chasing == true;
}
}
`

Thank you for your help Burdock. I already implemented a “lastSeen” location based on raycast - so the target switches from playerCar to lastSeen.transform.position. The problem comes afterwards.
Imagine you are following a car and lose sight of it. You wouldn’t just drive to the last location you’ve seen the car but rather to the next intersection, in hope that you can catch up and/or see the car again (also, my game takes place in a rural environment, so the next intersection can be kilometers away).

I basically need to extend the A* path from AIcar-lastSeen to AIcar-lastSeen-nextIntersection and I can’t use findClosest because the closest intersection could be behind the car. It has to be the closest intersection ahead.

Sorry if my description was unclear

Well, once your AI.chasing == false, you have a few things you can do~

#1: cheating AI until you get to the next intersection… Really!, This is not a bad way to do it

#2: find the closest intersection ahead VIA lanes (IE: each road has 2+ lanes)

#3: find the closest intersection with continue, AKA just follow this road until you hit an intersection

#1: I know what you mean, but so far I’ve been able to get realistic behaviour without cheating :slight_smile:

#2: I’ve got a single lane per road - one line of waypoints.

#3: Yes, but how?

Your suggestions inspired me for two ideas:

One idea would be to have numbered waypoints per road section (from intersectionA to intersectionB) and then figure out the direction with:

if((currentWaypoint# - nextWaypoint#) == 1)
target.position = intersectionA.position
else… intersectionB.position

Another idea would be to search for closest intersection, calculate a path to it and if the nextWaypoint is behind the car, cancel the path, search for the second closest intersection, calculate a path… and so forth, until the nextWaypoint is in fron of the car. But that’s a very clumsy way to do it.

My main, path specific question still remains: since I have a path from AIcar to lastSeenPosition, is it possible to extend that path until it reaches a node tagged “intersection”?