x


SphereCast and CheckSphere failing to catch collisions.

Basically I need to know what inner workings can make these checks false, even though everything I can see indicates they should be true. Details below:

Trying to make a gameobject warp to a point, but it must check for a valid destination first.

Step 1:
If a SphereCast hits something, use the hit point for step 2 (warps to closest edge of thick objects)
If not, use the spherecast termination point for step 2 (limits distance that can be covered by a warp)

Step 2:
Do a CheckSphere at the coordinates from step 1 for added insurance.
If it hits nothing, this should be a valid location to warp to.

And that's all wonderful when it works, but it frequently fails in this scenario:
Both checks always fail together (seemingly). I'll often wind up stuck inside thick objects if the desired warp point was close to the thick object's center. The failures seem to only occur when the warping object is in direct contact with the obstacle object. I can see that causing the SphereCast to fail, but it seems like a CheckSphere should realize it's completely inside of a BoxCollider, right?

None of the objects can move during the check. The checks are performed in one frame in FixedUpdate, and I've toyed with improving the physics solver quality and fixed timestep.

Any advice as to why this can fail?

more ▼

asked Jun 09 '12 at 12:21 AM

AlwaysSunny gravatar image

AlwaysSunny
283 14 20 26

Instead of using the word fail/succeeds, could you say, true/false. I think I know what you are saying, but my common sense is a lot worse than my programming.

I interpreted the first fail as the sphereCast returned false, and the second fail as the checkSphere returned true (therefore the position the player wanted to move to was occupied).

Jun 17 '12 at 05:19 AM Matt Downey
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First something that might give you a little insight.

Physics.SphereCast will return false if the imaginary sphere starts its "sweep" while already inside of an object.

Basically, you can think of Physics.SphereCast as:

if(!Physics.CheckSphere(transform.position,...)) //if the current position is not occupied
    {
    Physics.SphereCast(); //then cast the sphere as you would suspect
    }

Looking at why they would both fail, knowing this, I would guess that what is happening, is that from Physics.SphereCast() the default RaycastHit information is being returned: normal = Vector3(0,0,0), distance = 0, etc. This would mean the next check is checking at the origin of your map Vector3(0,0,0) in world space and looking for a collision within whatever radius. Try removing any colliders from the position Vector3(0,0,0) in world space and see if the second statement returns true every single time. If so then that was why the second statement was failing. The fix for the first statement is relatively hard, I would suggest using a smaller radius for the spherecast (by at least 0.11).

Furthermore: in edit (tab)-->project settings-->physics you can change the variable "min penetration for penalty" to a lower number like 0.01 or 0.005 (or lower although it is the law of diminishing returns) until the collisions are precise enough for your tastes, which should make everything work better. (If you do this, instead of subtracting 0.11 from the radius you can subtract 2*n + 0.01 instead.)

more ▼

answered Jun 17 '12 at 05:04 AM

Matt Downey gravatar image

Matt Downey
287 4 6 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2584
x1580
x38
x4

asked: Jun 09 '12 at 12:21 AM

Seen: 902 times

Last Updated: Jun 17 '12 at 05:26 AM