admin管理员组

文章数量:1389768

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.

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.

Share Improve this question edited Mar 14 at 20:55 Jiminion asked Mar 13 at 20:50 JiminionJiminion 5,1701 gold badge34 silver badges56 bronze badges 2
  • 1 how are you making your mask variable? in a field on the inspector, or by code, if by code can you share just so we can rule out that you arent actually blocking all the layers you expect, can you confirm what you're expecting to hit has a collider – BugFinder Commented Mar 13 at 22:01
  • @BugFinder mask is made in code. It is in the form: mask = LayerMask.GetMask("mask1", "mask2", mask3") I do have a collider. That is required, right? It is a box collider. I will check if it has the right properties. Thanks! – Jiminion Commented Mar 14 at 2:02
Add a comment  | 

2 Answers 2

Reset to default 2

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

  • confirm the layer mask

  • confirm the layer assigned to your object

  • confirm it has a collider

  • confirm that collider and visuals match

  • confirm the beginning of the raycast

    Notes: Raycasts will not detect Colliders for which the Raycast origin is inside the Collider

    except maybe if you enable Queries Hit Backfaces in the Physics Settings


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

(treat this as a comment ı don't have 50+ reputation) ı was using gizmos for debugging raycasts it is good to visually see the rays, make it change color based on some conditions etc.

Gizmos.DrawLine(transform.position, target.position);

本文标签: visibilityMy Unity project cannot see the new object I have added (physicsRaycast() )Stack Overflow