Collision detection (433110)

Hi,

I have a bunch of objects in my game with box colliders (triggers). They randomly spawn at 5 different locations. I want to make sure they don’t spawn on top of each other (ie if one tries to spawn on another one, choose one of the 4 other locations instead).

I’m thinking the best way to do this would be spawn the object, test for collision with another object, and if there is collision then move it to one of the other 4 locations, repeat the process until it finds a location with nothing. All this should happen before the draw call so the player won’t see anything.

My question is, how can I check the collision without having to use the OnTriggerEnter callback? I’m assuming this function would be called in the next frame, after the draw call. Is there anything like action script 3’s hitTestObject to test if the collider is currently colliding with something?

Also, if anyone thinks I’m way off the beaten track and should be doing this a different way, please enlighten me :slight_smile:

I’d just keep track of which spawn locations are in use, and save the time of trying to detect if something is there.

It only needs to be an array of booleans.

bool[] inUse = new bool[5] {false, false, false, false, false};

If you spawn something at location 0… inUse[0] = true and when the item is picked up inUse[0] = false;

The natural progression from that is to have a class called SpawnPoint that would have the transform position (saves finding it to Instantiate at), a reference to the created GameObject (for purposes of destroying or accessing) and maybe a timer for how long it exists.