Detect when ped is near the rear of the vehicle

Is there a way to detect when a ped or a vehicle is near the rear of another vehicle? I want to create a script that does it’s thing when when the player behind an NPC vehicle, or following it with a car.