Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Raycasting won't detect HitBox

Discussion in 'Project Tiny' started by Ghostbright, Mar 10, 2019.

  1. Ghostbright

    Ghostbright

    Joined:
    Sep 6, 2014
    Posts:
    5
    Hello all, I'm wondering if anyone can tell me if raycasting is currently broken or if I'm just using it wrong.

    I have RectHitBox2D components on a few entities and have tried:

    Code (JavaScript):
    1. this.world.forEach([ut.Core2D.Camera2D, ut.Entity],
    2.                 (cam, camEntity) =>
    3.                 {
    4.                     let ray = ut.HitBox2D.HitBox2DService.rayCast(this.world, new Vector3(-4.5, -1.5, 0), new Vector3(4.5, -1.5, 0), camEntity);
    5.  
    6.                     if(!ray.entityHit.isNone)
    7.                     {
    8.                         console.log("Ray hit.");
    9.                     }
    10.                     else
    11.                     {
    12.                         console.log("No ray hit.")
    13.                     }                  
    14.                 });
    This ray runs across three different entities, and yet they never register. Everything is on the same layer. Has anyone out there successfully used raycasting for anything yet?
     
  2. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    I had same issue . Maybe we are doing something wrong or raycast is broken :)
     
    Ghostbright likes this.
  3. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    62
    The
    entityHit.isNone
    is a method and should therefore be called like
    entityHit.isNone()
     
    Ghostbright and sniffle63 like this.
  4. Ghostbright

    Ghostbright

    Joined:
    Sep 6, 2014
    Posts:
    5
    This is it exactly! Thank you very much. Still trying to get a handle on TypeScript.
     
  5. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    119
    What components are you using to get the raycast to work?

    I tried it with BoxCollider2D and a Rigidbody2D

    Then i tried it with a RectHitBox2D (which says you have to have a sprite renderer to use)
    So i tried it with a Sprite2DrendererHitBox2D because it was saying i needed a sprite anyways

    Then i tried it with a RectHitBox2D and a Rigidbody2D

    All of those returned true for isNone() tho, so im wondering if im using the components wrong, or if i coded soemthign wrong.

    Altho im using the same code as OP
    Code (JavaScript):
    1.  this.world.forEach([ut.Core2D.Camera2D, ut.Entity], (camera, camEntity) => {
    2.  
    3.        
    4.                 let ray = ut.HitBox2D.HitBox2DService.rayCast(this.world, new Vector3(0, 1, 0), new Vector3(0, -2.5, 0), camEntity);
    5.                 if(ray.entityHit.isNone())
    6.                 {
    7.                     console.log("No Ray hit.");
    8.                 }
    9.                 else
    10.                 {
    11.                     console.log("Ray hit.")
    12.                 }
    13.             });
     
    Last edited: Mar 15, 2019
  6. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    119
    Seem to of figured it out.

    If your raycast hits a BoxCollider2D at ALL, it will ALWAYS return empty, regardless of if there is also a RectHitBox2D or not

    So its not possible to overlap a a RectHitBox2D onto a BoxCollider2D for raycasting into the ground to detect for when you land on the ground. But, you can use a hittest instead.


    *Edit*
    Maybe its not the BoxCollider2D, i think its if it hits an area where theres tilemaps drawn?

    *Edit*
    I have no idea what it is, just seems like it doesnt work at random places on the screen
     
    Last edited: Mar 16, 2019
    GliderGuy likes this.
  7. raymondyunity

    raymondyunity

    Unity Technologies

    Joined:
    Apr 30, 2018
    Posts:
    89
    Raycasts are part of the HitBox2D module and work with RectHitbox2D. BoxCollider is part of the Physics2D module. The Hitbox2D module was created for a lightweight hit detection and overlap solution for 2D sprites.
     
  8. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    119
    Im aware of that, i was pointing out that my Raycast randomly does not work when moving it around the screen while trying to raycast into a RectHitbox2D.

    At first i thought it was because i ALSO had BoxCollider2D , but i was mistaking as my edit pointed out.
     
    Zoelovezle likes this.