Search Unity

Batched raycasts can't hit anything?

Discussion in 'Data Oriented Technology Stack' started by illinar, Dec 19, 2018.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    I need help, I've been trying to make this work for days. I'm currently pretty sure I'm not doing anything wrong.

    The raycast commands seem to be correct, but they never hit anything.

    upload_2018-12-19_22-44-53.png

    Log output:

    upload_2018-12-19_22-47-46.png

    Always null. The scene is correct, there are static colliders, and also everything is inside a giant collider just in case, if raycasts hit from inside by default.

    Why no hits?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,429
    Are you shure scene is correct? Simple sample which hits sphere colliders on dummy and instantiate default sphere on hit point.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Unity.Collections;
    4. using Unity.Jobs;
    5.  
    6. public class RaycastTest : MonoBehaviour
    7. {
    8.     public GameObject prefab;
    9.     void Start()
    10.     {
    11.         var results = new NativeArray<RaycastHit>(11, Allocator.TempJob);
    12.         var commands = new NativeArray<RaycastCommand>(11, Allocator.TempJob);
    13.  
    14.         Vector3 origin = Vector3.zero;
    15.         var i = 0;
    16.         for (int x = -5; x <= 5; x++)
    17.         {
    18.             for (int y = 0; y <= 5; y++)
    19.             {
    20.                 if (Mathf.Pow(x - 0, 2) + Mathf.Pow(y - 0, 2) == 25 && i < 11)
    21.                 {
    22.                     Vector3 direction = new Vector3(x, y, 0);
    23.                     Debug.DrawRay(origin, direction * 2, Color.red, 100);
    24.                     commands[i] = new RaycastCommand(origin, direction * 2);
    25.                     i++;
    26.                 }
    27.                    
    28.             }
    29.         }
    30.        
    31.         JobHandle handle = RaycastCommand.ScheduleBatch(commands, results, 0);
    32.  
    33.         handle.Complete();
    34.  
    35.         for (int j = 0; j < results.Length; j++)
    36.         {
    37.             RaycastHit hit = results[j];
    38.  
    39.             if (hit.collider != null)
    40.             {
    41.                 Debug.Log(hit.collider.name);
    42.                 Instantiate(prefab, hit.point, Quaternion.identity);
    43.             }
    44.         }
    45.  
    46.         results.Dispose();
    47.         commands.Dispose();
    48.     }
    49. }
    50.  
    51.  
    upload_2018-12-19_23-29-43.png
     
    hippocoder likes this.
  3. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    Sorry, I didn't realize I'm on the main thread and can draw rays. Also.. make a normal raycast. Which I did now.

    upload_2018-12-20_7-44-19.png

    upload_2018-12-20_7-45-59.png

    upload_2018-12-20_7-42-35.png

    So, the hits aren't written into, or the raycasts are incorrect.
     
    Last edited: Dec 20, 2018
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,663
    So it doesn't really answer your question but just a note about your code, from documentation

    You need to stop your loop once you find the first null collider otherwise you might get invalid results .

    -edit-

    is your command direction normalized?
     
    Last edited: Dec 20, 2018
  5. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    This is about the first hit of a given raycast when there are multiple hits per raycast. I have only one hit per raycast.

    Good thought. Got me hopeful for a moment that this could be it, but alas. Tried normalized, same result.
     
  6. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    Okay, I'm sorry this is dumb... The default number of hits is zero, and I had the default. WHO NEEDS ZERO HITS? :D So you create commands, you send them into a job, and you get NO RESULTS back. What's the point? It's not just why 1 hit is not the default, it's why have zero ever at all?

    Now I set it explicitly to have 1 hit per raycast and it's working.

    Thanks, everyone.
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,429
    only for maxHits > 1 and this behaviour not ready yet
    It's not required, look at my ansver above
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,429
    What? You can set maxHits in RycastCommand (1 by default), and in ScheduleBatch you sets min commands per job (even if it 0 all works, as you can see in my code above) where you find 1 hit per raycast?
     
    Last edited: Dec 20, 2018
  9. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    upload_2018-12-20_10-18-44.png

    Well, that's definitely how it works for me. If I don't set it to 1, it doesn't work, as if it's default is zero.
     
  10. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    1,429
    This is not
    it's maximum hits limit:)
    and maxHits is 1 by default:
    upload_2018-12-20_10-35-51.png

    Edit:
    Oh I see you use default constructor :) This is why it's 0. Why not use normal constructor? Besides it's shorter :)
     
  11. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    563
    Yeah, I see. I got used to those constructors, seems more readable, the extra lines are a trade-off, yes.

    upload_2018-12-20_10-54-21.png
     
  12. temps12

    temps12

    Joined:
    Nov 28, 2014
    Posts:
    15
    I've been stung on missing to set a field in a struct many times so thats why I usually dont assign the struct on declaration. Instead just declare it and fill in the fields. That way it throws compile errors if there is something unassigned. It also helps when adding new fields to a struct that is used in the code, it'll give compile errors everywhere it is used.

    But since started playing with ECS I've been using new Struct() { asd = 1}; a lot because of laziness but Im probably gonna pay for that later :)
     
    illinar likes this.