Search Unity

Raycast Inaccuracy

Discussion in 'Physics for ECS' started by Ahndrakhul, Feb 4, 2020.

  1. Ahndrakhul

    Ahndrakhul

    Joined:
    Mar 7, 2015
    Posts:
    23
    Edit: Problem was caused by giving collisionWorld.Castray an origin and a direction when it requires an origin and an endpoint. It works like a linecast instead of the old physics raycast.

    I'm returning to Unity and new to DOTS physics, so I'm probably doing something stupid here (possibly even something completely unrelated to the physics itself). I am using a raycaster based on the one in the collision queries section of the manual and this youtube tutorial. It works pretty well as long as the objects I'm casting against are at the scene origin, but the farther I move away from it, the more inaccurate the casts become. For example, if I move a cube from scene origin to an X position of 25, I have to move the mouse pointer completely off the cube in the X direction for the raycast to detect anything.

    Raycast Code:

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Physics;
    4. using Unity.Physics.Systems;
    5.  
    6. public static class TestRaycast
    7. {
    8.     public static Entity Raycast(float3 fromPosition, float3 toPosition)
    9.     {
    10.         BuildPhysicsWorld buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
    11.         CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
    12.  
    13.         RaycastInput raycastInput = new RaycastInput
    14.         {
    15.             Start = fromPosition,
    16.             End = toPosition,
    17.             Filter = new CollisionFilter()
    18.             {
    19.                 BelongsTo = ~0u,
    20.                 CollidesWith = ~0u,
    21.                 GroupIndex = 0
    22.             }
    23.         };
    24.  
    25.         RaycastHit raycastHit = new RaycastHit();
    26.  
    27.         if (collisionWorld.CastRay(raycastInput, out raycastHit))
    28.         {
    29.             return buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
    30.         }
    31.         else
    32.         {
    33.             return Entity.Null;
    34.         }
    35.     }
    36. }
    How I call it:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DoRaycast : MonoBehaviour
    4. {
    5.     void Update()
    6.     {
    7.         if (Input.GetMouseButton(0))
    8.         {
    9.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.             float rayDistance = 100f;
    11.  
    12.             Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.green, 10);
    13.             Debug.LogError(TestRaycast.Raycast(ray.origin, ray.direction * rayDistance));
    14.         }
    15.     }
    16. }
    Can anyone see anything obviously wrong with this? Using the normal raycaster against normal physics objects with these inputs works just fine, but it seems like I must be screwing up the inputs to CollisionWorld.CastRay somehow. Drawing a ray with the same inputs displays a line passing straight through the objects I'm trying to cast against, but I get NULL from my raycast method.
     
    Last edited: Feb 5, 2020
  2. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    I think that should be TestRaycast.Raycast(ray.origin, ray.origin + ray.direction * rayDistance)
     
    Pine, lclemens and Ahndrakhul like this.
  3. Ahndrakhul

    Ahndrakhul

    Joined:
    Mar 7, 2015
    Posts:
    23
    Thank you! That does work. It doesn't look like it should, because if I use that version to draw rays on the screen, they completely miss my objects, but the colliders are being hit anyway. This is what it looks like in the editor when I draw rays using those different inputs:

    DrawRays.png

    I'm too brain dead right now to figure out why it works, but I'll look at it again tomorrow.
    Thanks again.
     
    Last edited: Feb 5, 2020
  4. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    I'm having the same problem as you; as per this thread: https://forum.unity.com/threads/raycast-misses-custom-mesh-collider.810657/#post-5408568

    I'm not sure if it's my colliders; the raycast stuff **looks** right, and the Debug.DrawRay convinced me I was doing it correctly, but now I'm not sure. Like you, I've tried both versions of the ray code as per above, and with short ray distances, they shoot out at different angles, with a longer distance, the rays are both similarly accurate, but in my case the correct colliders are still not being hit.. :(
     
    Ahndrakhul likes this.
  5. Ahndrakhul

    Ahndrakhul

    Joined:
    Mar 7, 2015
    Posts:
    23

    I looked at your thread and was happy to see that you solved your problem. In my case, the whole issue turned out to be that CollisionWorld.Castray works like a linecast and not a raycast. It expects an origin and an endpoint instead of an origin and a direction.
    ray.origin + ray.direction * rayDistance 
    gives the endpoint, so it works for a linecast (and thus for Castray), but in drawray it created those red lines that went off to nowhere. That's what I get for not paying attention to RaycastInput's field names I guess.
     
    Last edited: Feb 5, 2020
    steveeHavok likes this.