Hi,
I am making a racing game with AI vehicles but I’m not sure how to make a finish line that says whether you are 1st, 2nd or third does anyone know how I can do this?
I used trigger boxes for this. There is some criteria that you have to take in account.
There should be a map data structure that stores all the vehicles in the game and a float variable for each one called Value
I will explain what this value will be used for.
Let us first distribute some box colliders called checkpoints along the lap, having one of them placed in the finish/start line… Every box collider will be literally programmed as a Checkpoint in a way in which whenever it gets overlapped by a vehicle that has an integer variable called Checkpoint
, the number of the last checkpoint they overlapped along the track will be stored in that variable. So whenever we have the overlapping event between each vehicle and a Checkpoint i, we have to check if the vehicle Checkpoint
value equals to i-1 and if so, we proceed adding +1 to the vehicle Checkpoint
value. This way ensure that we are going through the lap in a particular order and without taking shortcuts by skipping checkpoints.
After passing a lap, the checkpoint placed in the finish/start line will increase another integer variable of the overlapped vehicle called Lap
that stores the lap in which the vehicle is placed, this will occur only if the vehicle has passed through all the checkpoints in the lap.
There is one last value called CPDistance
for each vehicle, that stores the distance from the last checkpoint they overlapped to the current position.
With this said, we can follow this criteria in order to get they sorted as 1st, 2nd, and 3rd place:
EDIT: The variable value
that I mentioned in the beginning is:
Value = Lap + Checkpoint/10 + CPDistance/1000
Then every vehicle will have its Value
depending on the point they are in the race.
If we sort the map data structure by Value
, we will get the order in which we are placed in the race in real time.
I hope this post is useful!
I don’t really understand this could you explain further and have an example?
Let’s see the solution this way:
-
The vehicle that gets the first place is the one that is in the greatest lap.
-
If the vehicles are in the same lap, the first place is for the one that passed the greatest checkpoint.
-
If the vehicles are in the same lap and in the same checkpoint, the first place is for the one that is closer to the next checkpoint.
This is the main criteria. I just gave my own implementation for it.
Let me explain with this example:
We have 4 vehicles: A, B, C and D.
Let us suppose this:
Vehicle A is in the 2nd Lap, Checkpoint 1, and its distance from Checkpoint 1 is 3.5m.
Which means that for Vehicle A:
Lap = 2
Checkpoint = 1
CPDistance = 3.5
Let me correct my formula from my first response and use this formula instead
//For vehicle A
Value = Lap + Checkpoint/10 + CPDistance/1000
Value = 2 + 0.1 + 0.0035
Value = 2.1035
For vehicle B, let us suppose, it is in Lap 3, checkpoint 2, and its distance from that checkpoint is 5.32m, using the same logic we have
//For vehicle B
Value = Lap + Checkpoint/10 + CPDistance/1000
Value = 3 + 0.2 + 0.00532
Value = 3.20532
Now, we see Vehicles C and D are in the same checkpoint 3 and let’s assume that they both are in the Lap 1 . Calculating their distance from Chackpoint 3, let’s say C is 5.8m away from Checkpoint 3, and D is 10.1m away from Checkpoint 3.
//For vehicle C
Value = Lap + Checkpoint/10 + CPDistance/1000
Value = 1 + 0.3 + 0.0058
Value = 1.3058
//For vehicle D
Value = Lap + Checkpoint/10 + CPDistance/1000
Value = 1 + 0.3 + 0.0101
Value = 1.3101
So we have the values:
Value = 2.1035 //for A
Value = 3.20532 //for B
Value = 1.3058 //for C
Value = 1.3101 //for D
Sorting this will give us
Value = 3.20532 //for B......... 1st PLACE
Value = 2.1035 //for A......... 2nd PLACE
Value = 1.3101 //for D......... 3rd PLACE
Value = 1.3058 //for C......... 4th PLACE
And with that, we know the correct order for the vehicles.
Ok how would I make the part that scans who is closer to the next check point though (in blueprint)?