unity-game-enginevisibilitymesh-colliderraycast

My Unity project cannot see the new object I have added (physics.Raycast() )


I am using

physics.Raycast(origin,direction, distance, mask, queryTrigger)

Origin is the transform of the camera.

Direction is Origin transform minus the transform of the visible point. (EDIT: This is wrong. It is actually (visible point - origin). )

Distance is distance between origin and visible point, plus some extra.

Mask is a mask of the visible point layers. query Trigger is set to true.

I use the call to determine if the object is 'visible' as in close proximity to the camera/user. This is a 3D project.

This is what is done with the other existing objects in the simulation. Each object has a number of points around it (transforms) that are used to test for or confirm visibility. The points are used to direct where the Raycast() should look. (The other point is the camera center.)

What I don't understand is how Raycast() works. I am not sure what I am doing wrong for this new object. Is Raycast() looking for an associated collider to intersect with? Is it important for the visible points to be outside of the collider box/sphere/capsule? Is making the search distance longer likely to help?

Raycast() only returns true or false so it is difficult to understand why it is failing.


Solution

  • Direction is Origin transform minus the transform of the visible point.

    if that is true then your direction points backwards! A vector direction is always

    targetPoint - origin point
    

    not the other way round


    Is Raycast() looking for an associated collider to intersect with?

    Of course! That's exactly what a Physics.Raycast does

    Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.


    Is it important for the visible points to be outside of the collider box/sphere/capsule?

    I don't quite understand that point. The Raycast works on a pure physics (collider) level and doesn't know/care about any visuals. If you setup your layers correctly it also wouldn't matter whether your target object sits within/behind another bigger collider as long as that bigger collider is being ignored by the raycast layer mask.


    Without more information about your exact code, layer settings and scene setup it is pretty hard to tell you more.

    I would start with the typical suspects


    In general for checking visibility you might also go the other way round, use a LineCast between camera and target point. If you hit anything else except the target object then something was blocking your line cast -> potentially not visible