Physics.RayCast dosent work.

Hi, Im now using unity Physics.RayCastAll to test the hit things. but it always returns nothing and I cant fingerout what the problem should be.

here is the usage code.

    var ray = CreateARayMethod();
    hits = Physics.RaycastAll(ray, ushort.MaxValue, Physics.AllLayers);
    Debug.DrawRay(ray.GetPoint(0), ray.direction * 120, Color.magenta);

the hit always returns nothing, and i sure that the ray was absolutely go through a collider.
like this.
9526114--1344211--1320313-dbcd552ac661d1fe4b506c56abd988c8.png

Looks like you’re using the 3d Physics class, and not the Physics2D class for 2d physics.

Actually it was the 3D box Collider with orthographic view. :slight_smile: ,it look not so clearly in thee-D view.

If you’re working in 3D then that screenshot isn’t massively helpful - the ray could be starting and finishing at the wrong depth?

What’s happening in the CreateARayMethod method?

The ray was create by a snap-window mouse click event. I dont wanna put it on this page because the method is too complicated and I think I had draw it friendly on scene and take a screenshot. Actually the ray is only contruct with start point and a direction. In this case start with camera position and a dir value so it dosent matter.
The problem is , is the physic scene in unity Physics system matter? After shortly read the source code , it gose to ‘PhysicsScene.Raycast’ , can not continue without native debug experience. :frowning:

   public bool Raycast(Vector3 origin, Vector3 direction, [DefaultValue("Mathf.Infinity")] float maxDistance = float.PositiveInfinity, [DefaultValue("Physics.DefaultRaycastLayers")] int layerMask = -5, [DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal)
   {
       float magnitude = direction.magnitude;
       if (magnitude > float.Epsilon)
       {
           Vector3 direction2 = direction / magnitude;
           return Internal_RaycastTest(ray: new Ray(origin, direction2), physicsScene: this, maxDistance: maxDistance, layerMask: layerMask, queryTriggerInteraction: queryTriggerInteraction);
       }

       return false;
   }

I guess the colliders are not trigger colliders? Or you can,

Physics.RaycastAll(ray, Mathf.Infinity, Physics.AllLayers, Physics.queriesHitTriggers.Collide);

if they are.

After moving assets to a new blank project and rebuild all cache, it solved. What a wired bug. Thank you anyway. :wink:

Might have been something in your project settings then.