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

Unity Bug: Physics.Linecast returns *last* collision Unity 5.5.1

Discussion in 'Physics' started by TheTaj, Feb 22, 2017.

  1. TheTaj

    TheTaj

    Joined:
    Jun 26, 2014
    Posts:
    24


    Hello fellow devs!
    I've come across a really frustrating bug while porting my VR game onto the Samsung Gear (game is already on the Google Play store [Wizard Stole My Goose] and working just fine for the Google Daydream from Unity 5.4.2GVR-12) using Unity 5.5.1 (I'm pretty sure its the latest version).
    Basically, I have a gameobject with a trigger box-collider, and a child gameobject with a trigger sphere-collider that encircles the entirety of the gameobject. Both have rigidbodies and *were* detecting collision correctly until the port, now what happens is the raycast (and linecast, I tried modifying it to take a linecast instead with the same results) randomly returns either the sphere or the box-collider (the box collider is completely inside of the sphere collider, it should be impossible to reach without first colliding with the sphere collider).

    In short: Both Raycast and Linecast are returning collisions towards the end of the line/ray, as opposed to the *first* collision,

    Now, granted, it might be possible for me to work around this by giving the box-collider a layer that is ignored by the raycast, but this solution breaks down later when I have 2 objects that should be interactable and the raycast starts randomly returning the one behind the other.

    Basically, can anyone tell me why the raycast isnt returning the first collision? Or if there's another method that is more precise in returning the *first* collision?

    Code (CSharp):
    1. //Ray raycast = new Ray(cursor.transform.position, cursor.transform.forward);
    2.         Vector3 aim = cursor.transform.position + cursor.transform.forward * 300;
    3.         RaycastHit hit;
    4.         if (Physics.Linecast(cursor.transform.position, aim, out hit))
    5.         {  ...
     
    Last edited: Feb 24, 2017
  2. TheTaj

    TheTaj

    Joined:
    Jun 26, 2014
    Posts:
    24
    No responses yet...
    I just want to confirm that I'm increasingly more certain that this is a problem with Unity itself and not my code.


    Code (CSharp):
    1. Vector3 aim = realControllerPivot.transform.position + cursor.transform.forward * 300;
    2.         RaycastHit hit;
    3.         if (Physics.Linecast(cursor.transform.position, aim, out hit))
    4.         {
    5.             RaycastHit temp;
    6.             temp = hit;
    7.  
    8.             RaycastHit bullshit;
    9.             if(Physics.Linecast(realControllerPivot.transform.position, hit.transform.position, out bullshit)){
    10.                 if (bullshit.transform.gameObject != hit.transform.gameObject) {
    11.                     Debug.Log ("How is this possible, Unity?");
    12.                     Debug.Log ("First it hit: " + hit.transform.gameObject + " then it hit: " + bullshit.transform.gameObject);
    13.                     temp = bullshit;
    14.                 }
    15.             }
    16.            
    Testing this in the editor and it repeatedly returns the Debug.Log confirming that there are ignored collisions inbetween the collision point the linecast is returning.

    Since I don't feel like sitting on my thumbs waiting for another unity update, any cheap workarounds here? So far, I'm thinking I'll just have to send multiple linecasts each frame and grab the nearest one (which is stupidly inefficient, but whatever i guess...)