- Home /
How to respawn player car in war track
After watching a rewarded video, I want to reward a game player with an extra life in gameplay. So for this, I have again respawned player car again within the track. But sometimes this kind of situation get occurred:
Player Car gets overlapped with other AI cars. Total 9 to 13 cars were moving on the same track, so what strategy I require to apply in respawning? So that player car doesn't overlap other moving cars.
Answer by siddharth3322 · Mar 29, 2019 at 04:50 PM
Okay, I managed to solve the problem:
Performed these steps:
Created multiple respawn positions on the track
Then at respawn time, checked all cars position respective to currently selected spawn position
At last, whichever position get selected that is safe for respawning
Here is the code that I used for this purpose:
private Vector3 FindSafePlaceForSpawning()
{
Vector3 spawnPosition = playerRespawnOptions[0].position;
for (int i = 0; i < playerRespawnOptions.Length; i++)
{
bool isAnyNearCar = false;
for (int j = 0; j < gamePlayCarList.Count; j++)
{
if (Vector3.Distance(playerRespawnOptions[i].position, gamePlayCarList[j].position) < 2f)
{
isAnyNearCar = true;
break;
}
}
if (!isAnyNearCar)
spawnPosition = playerRespawnOptions[i].position;
}
return spawnPosition;
}
Your answer
Follow this Question
Related Questions
Problem with the randomly spawning enemies 0 Answers
How do I make enemies not spawn in the same position? 2 Answers
Preventing Spawn Overlap With Spheres 1 Answer
[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers
How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers