Collider2D.ClosestPoint() returns the input argument instead of closest point on the collider

I have the following line of code that doesn’t seem to work always. I have one in a collision event that works fine and one in FixedUpdate that returns wrong value, which is the input argument. I know the point is not inside the collider, not even close. I don’t really understand why the line of code doesn’t work in FixedUpdate, but does in a collision event.

Vector2 closestPoint = connectedBodyCollider.ClosestPoint(toolParent.ShootPosition.position);

According to the documentation, there are two cases where the method can return the input position:

In the case where the position is inside this Collider or this Collider is disabled, then the input position is returned instead.

In a collision callback you’re guaranteed the collider is enabled. But in FixedUpdate you might need to explicitly check if the collider is enabled (and active)?

Yeah, the collider was disabled. Returning the input value in that case is not the most intuitive result, though…

What would be intuitive? There’s not much else it can do. It’s even documented behaviour.

With the way the function is currently written there might not be anything else. Generally if a function has a fail condition, if you don’t want to raise an exception, it should return a boolean to indicate success and have out argument for the result. One option is to add a TryGetClosestPoint function that works like I described.

I’m aware of alternative but TBH for this, I think that’s complete overkill. We also generally avoid using out args and throwing exceptions.

Also, no queries at all work if the collider isn’t enabled so that would mean doing what you suggested for all of them as a standard. Frankly, reading the docs is something you should try to first do when using something.