I’m checking my spawn position through raycasting so that I don’t spawn on top of the player or another spawnable object. It works pretty well but there will be times when the sides of the player and spawned object overlap.
Here is the method i’m currently using: spawnPosIsLegal(Vector3 pos) - Pastebin.com
The suggested Physics.OverlapSphere method is the right way to go.
I would recommend to rewrite your method like this:
private bool spawnPosIsLegal(Vector3 pos, float radius = 1)
{
Collider[] colliders = Physics.OverlapSphere(pos, radius);
foreach(Collider collider in colliders)
{
GameObject go = collider.gameObject;
if (go.transform.CompareTag("Player"))
{
return false;
}
}
return true;
}
You can use the optional parameter radius to specify the safe radius around a given position. When there’s no radius given, one meter/unit is used.