Physics.CheckSphere always returning false

Hi,
I am trying to get Physics.CheckSphere to work, but it doesn’t seem to matter what layer mask I use or what radius I use, it never registers an object with a collider. I’m just trying to test the script out right now so my code is pretty simple.

public float radiusCircle = 2400f;
	public bool isSomethingThere = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if (Physics.CheckSphere (transform.position, radiusCircle, 1 << 12)) {
			isSomethingThere = true;
			Debug.Log("There is something there");
		}
	}

Silly question, but are you sure there are any objects within N units of transform.position, and that the layers match? Also, anything that has anything to do with physics belongs in FixedUpdate() - though it might work in Update() 9 out of 10 times, it belongs in FixedUpdate()

Whenever you have a problems like this one, eliminate possibilities. Remove the '1 << 12'. Does it work? Hard-code a value for 'radiusCircle'. Note that the initialization of public variables like you do on line 1 is only used when the script is attached. After that, only the value in the Inspector matters.

1 Answer

1

Ok, so I feel VERY silly for not noticing this before, but my colliders are 2D, but of course the checksphere only registers 3D colliders. So I slapped a 3D collider on my object, set the z-scale to 0 and it worked!
In the future, it might be helpful for the documentation to note what won’t work with 2D colliders since that might trip up newbies like me.

Is there any way not to use colliders3d on 2d objects and find method like this one ur using?

The closest thing might be Physics2D.OverlapCircle