- Home /
How do I get all colliders within a radius at a given point?
I want to make sure a sphere is not overlapping any other objects. When it is, I want to move it away from the overlapping collider.
My current code is:
RaycastHit intersectingHit;
while(Physics.SphereCast(hand.transform.position, handRadius, Vector3.forward, out intersectingHit, 0.01f, collisionMask)){
Vector3 direction = intersectingHit.point - hand.transform.position;
if(direction != Vector3.zero){
hand.transform.position = hand.transform.position - direction.normalized * 0.1f;
}
}
But spherecast doesn't seem to do the trick. Any ideas?
Answer by robertbu · May 06, 2013 at 04:55 AM
Sounds like you want Physics.OverlapSphere(). It returns a list of colliders. If you just want to check to see if there is an overlap, you can use Physics.CheckSphere().
OverlapSphere doesn't work for me (unity freezes), and CheckSphere is not usable, since I don't know which way I have to move.
I've never heard of unity freezing with an OverlapSphere(). Are you getting any errors? Can you post the code?
I just ran a quick test with OverlapSphere...1000 game objects, a large radius check from a moving position calculated every Update(). I saw zero impact on frame rate. Back out your other code and just do the OverlapSphere.