- Home /
Other
check if position is empty before spawn
Hi guys, I have three game objects and three locations, and I want to respawn them in a different location. Each time after destroying them, is there a way to check if the position is empty before respawning them? Thanks
Answer by bdubbert · Jul 26, 2021 at 07:04 PM
There are a couple different approaches you could use.
Check if there is physically any object occupying the space by using something like Physics.SphereCastAll to check for colliders around the space. Upsides: quickest to implement Downsides these spaces can be blocked by any colliders, and this method is more CPU intensive.
You can just keep track of which spaces are occupied manually, for example having a
Dictionary<Vector3, GameObject>
that you use to associate each position with the game object that is occupying it. So when you spawn an object you can set that position as occupied, and when you destroy an object you can set the position's corresponding object to null
Follow this Question
Related Questions
GameObject.FindGameObjectWithTag not finding the tagged objects. 1 Answer
How to smooth out character Roll in a Top-down 2D movement 0 Answers
Accurate Text 0 Answers
how to make a bullet spread ( shotgun ) in unity 2D 3 Answers
How can i spawn and Despawn for a specific time using this script? 2 Answers