Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Physics Raycast failing to cast

Discussion in 'Physics for ECS' started by Crafted_King, Mar 8, 2021.

  1. Crafted_King

    Crafted_King

    Joined:
    Feb 6, 2019
    Posts:
    3
    I have attempting to create a simple point and click teleportation system for a project I am working on. In order to perform the teleport I want to use a Physics raycast to find the position to move to. However, after experimenting with the code provided on the Unity Physics collision queries page, and the code from the Physics samples project, and my own attempts at implementation, I have been unable to get the the raycast to preform.

    Code (CSharp):
    1. Entities
    2.                 .WithName("LocomotionTeleportation")
    3.                 .WithoutBurst()
    4.                 .ForEach((Entity entity, int entityInQueryIndex, int nativeThreadIndex, in Translation translation, in Rotation rotation, in LocomotionTeleportationInputData input, in LocomotionTeleportationData teleportationData) => {
    5.                     if (input.enableTeleport == 1) {
    6.  
    7.                         var startPos = translation.Value;
    8.                         var endPos = translation.Value + math.forward(rotation.Value) * teleportationData.distance;
    9.                    
    10.                         var rayInput = new RaycastInput() {
    11.                             Start = startPos,
    12.                             End = endPos,
    13.                             Filter = new CollisionFilter() {
    14.                                 BelongsTo = ~0u,
    15.                                 CollidesWith = ~0u,
    16.                                 GroupIndex = 0
    17.                             }
    18.                         };
    19.            
    20.                         if (input.engageTeleport == 1) {
    21.                             bool didCast = collisionWorld.CastRay(rayInput, out var hit);
    22.  
    23.                             if (!didCast) {
    24.                                 Debug.Log("Failed to cast ray");
    25.                             }
    26.  
    27.                             Debug.Log(hit.Entity);
    28.                         }
    29.                     }
    30.                 }).Schedule();
    No matter what I change about the code, the collisionWorld.CastRay() method never seems to get performed, only ever producing a Debug log stating the failure, as I have defined.
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @Crafted_King , CastRay() is returning false because the ray is not hitting any physics body. I'd advise debugging to see why it's not hitting, maybe teleportationData.distance is too small or some other input makes the ray miss.
     
  3. Crafted_King

    Crafted_King

    Joined:
    Feb 6, 2019
    Posts:
    3
    I the scene setup to have a plane as the ground with a physics body, and a physics shape, so something should be returned when the ray crosses it. I also setup a indicator to show the end position of the ray (the indicator has no physics body or shape so it shouldn't be interacted with). But even without the indicator the ray still doesnt not cast

    Code (CSharp):
    1.  
    2. Entities
    3.                 .WithName("LocomotionTeleportIndicator")
    4.                 .WithBurst()
    5.                 .ForEach((in LocomotionTeleportationData teleportData, in LocomotionTeleportationInputData inputData, in Translation translation, in Rotation rotation) => {
    6.  
    7.                     var indicator = teleportData.indicator;
    8.  
    9.                     if (inputData.enableTeleport == 1 && HasComponent<Disabled>(indicator)) {
    10.                         ecb.RemoveComponent<Disabled>(indicator);
    11.                     }
    12.                     else if (inputData.enableTeleport == 0 && !HasComponent<Disabled>(indicator)) {
    13.                         ecb.AddComponent<Disabled>(indicator);
    14.                     }
    15.  
    16.                     if (inputData.enableTeleport == 1) {
    17.                         var position = translation.Value + math.forward(rotation.Value) * teleportData.distance;
    18.                         ecb.SetComponent(indicator, new Translation() {Value = position});
    19.                     }
    20.                  
    21.                 }).Schedule();
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Did you try to draw the indicator and enable draw colliders/collider edges in Physics Debug Display, to see if they intersect?
    You could also debug your code (turn burst off and use Run instead of Schedule to make it simpler) and step into the raycast function to see why it's missing. It might also be due to filters (what's the filter on ground object?), debugging will show.
    I don't see any obvious problems with the code you sent, but raycast also depends on the system you're running from, ground object setup, etc...
     
  5. JTAGames

    JTAGames

    Joined:
    Mar 7, 2019
    Posts:
    13


    This tutorial shows you just how to do that.