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 Raycast hit returns information based on the order the objects got hit

Discussion in 'Physics' started by SassyPantsy, Jun 28, 2023.

  1. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    136
    Here's an odd scenario,

    I perform a forward raycast from an object that rotates at a steady rate on the Y axis. In front of it, from closes to furthest, are a cube, which sit on the interactables layerMask, and a border, which sits on the border layerMask.

    This is the rayacst signature I use:

    Code (CSharp):
    1. if (Physics.Raycast(laserGun.position, transform.forward, out var hit, range, targetLayer))
    2.             {
    3.  
    4.                 int hitLayerMask;
    5.  
    6.                 // Shift a bit to the left by the layer number of the hit object to create a bitmask
    7.                 hitLayerMask = 1 << hit.transform.gameObject.layer;
    8.  
    9.                 // Perform bitwise AND operation with the target layer
    10.                 if ((targetLayer.value & hitLayerMask) != 0)
    11.                 {
    12.                     // The hit object's layer is in the target layer mask
    13.                 }
    14.                 hitPosition = hit.point;
    15.  
    16.             }

    Both interactables and border are selected inside the targetLayer variable, from the inspector.

    Now, I expect my raycast hit to return information on the cube, that is in the interactables layer, since it is closest to the raycast's origin. That does happen, unless I hit the border behind it at any stage.
    Once I hit the border behind it, no matter what, my raycast will 'ignore' the interactable cube and just go straight to the border, I have no idea why. Even if I perform a RaycastAll, I get 4 hits, all of their points are at the exact same position, which is the border's position.

    To be perfectly clear about my end goal, I'm shooting a laser from an origin point until it hits something. I want the laser's graphic to end in the hit point. Since what I described happens, it looks as if my laser passes through the object.

    Does anybody have any idea on why this might happen?

    These raycasts are performed every fixed update, based on an on key held

    Thanks
     
  2. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    136
    Classically, I've solved it. But it took two days to find the solution, so this thread was justified.
    I'm gonna keep this up so that people might see:

    apparently, since my laser object was 'stretching' to fill the distance between the hit point and the origin point, the laserGun's transform's origin point, i.e the pivot point, was beyond the cube, and now closer to the border.

    Changed the raycast's origin point to be on a static game object, and now everything works fine.