Physics.Raycast hitting when it shouldnt? :o

3366437--263811--upload_2018-1-24_15-14-39.png

Sorry I can’t show more of an image, but the project is currently under wraps. I have a very strange issue that seemingly shows Physics.RayCast not working (it sometimes reports a hit when it clearly isnt, and sometimes not when it clearly is) and just wondered if anyone knew anything obvious with the limited info I can give.

The basic code is :-
bool hit =
Physics.Raycast(_rayStartPoint, _rayDirection, out rh, _rayScanRange, layerMask, QueryTriggerInteraction.Ignore);

and the Green debug line is drawn with
Debug.DrawRay(_rayStartPoint, _rayDirection*5, Color.green,2,false);

The red line (cross) is drawn at the point Unity thinks the collision happens (rh.point).

However… the object it reports is being hit, is that green capsule collider you can see. (which clearly isn’t being hit by the line)

The capsule collider is nested in a few game objects, that ultimately move up and down a little, though no strange scale offsets are happening.

If there is anything obvious in physics settings etc that people can suggest, much appreciated. Other wise it currently looks like a Unity bug? I get the issue in both 2017.1 and 2017.3 (both Windows)

You’ll find it’s your code, and you’ll know why when you make a reproduction project, ie just a raycast test in isolation. This happens to everyone. It’s not bugged.

Don’t forget, if playing with timescale or pausing, don’t expect the visual of the colliders to match where they are. Collider transforms are only updated each fixed update if automatically updating.

Hi Hippocoder, Thanks for your reply, so to confirm, even though the Debug.DrawRay command, visually shows the line that I’m using in Physics.RayCast, and it can be clearly seen in the small screen grab that the line does not pass through the collider. It’s likely my code rather than Physics.RayCast being insolent by saying the collider is being hit?

… and, you are right :slight_smile:

What it was, twas there was some code that set the collider gameobject localposition to zero in Update.

Then in Late Update, it set it to another position.

My guess is, the physics part of it was triggering inbetween Update and LateUpdate.

Thanks :slight_smile: