- Home /
that items doesnt spawn in player position
Hey i have a SpawnManager which generates a RandomSpawnPosition for (for example) a death item. (If you catch it you die). The Problem is that it sometimes happens that this Item randomly spawns into you and you die without any chance.
that method generates the spawnPosition:
Vector3 GenerateSpawnPos() { float spawnPosX = Random.Range(-spawnRange, spawnRange); float spawnPosZ = Random.Range(-spawnRange, spawnRange); Vector3 randomPos = new Vector3(spawnPosX, 10, spawnPosZ); return randomPos; }
and that is how i summon (for example) an item:
Instantiate(destroyerUpPrefab, GenerateSpawnPosPowerUp(), destroyerUpPrefab.transform.rotation)
Answer by KoenigX3 · May 29, 2020 at 06:21 PM
Before you spawn the object, check if the generated position is near the player's position.
float threshold = 2;
void Spawn()
{
Vector3 itemPosition = GenerateSpawnPos();
while(Vector3.Distance(character.position, itemPosition) < threshold) itemPosition = GenerateSpawnPos();
Instantiate(destroyerUpPrefab, itemPosition, destroyerUpPrefab.transform.rotation);
}
Use threshold to define the minimal distance between the player and the item.
Nice $$anonymous$$ate it's working. Thanks for your help :)
Your answer
Follow this Question
Related Questions
How Instantiate Object Inside of Object and Instantiate Parent Object On Looping. 0 Answers
Multiple spawn location with random game object in random time spawn c# 1 Answer
Why this error when trying to instantiate object ?(Solved) 2 Answers
Instantiate issues 1 Answer
Spawn an object to a random spawn point from a list 2 Answers