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

Bug SERIOUS issue with Raycast/Linecast

Discussion in 'Physics' started by valentin56610, Jan 14, 2023.

  1. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    147
    Hello there

    It's been some time now I'm noticing VERY, VERY odd behaviour from Physics.Raycast and Physics.Linecast

    I am using the 3 parameters raycast, where parameter 1 is the origin, parameter 2 the direction and parameter 3 the maxDistance

    Could anybody explain to me WHY when using this setup it says that my character is grounded at all time (even when flying in the air) BUT when I use the 4 parameters Raycast, my character is NO LONGER grounded?

    Code that returns my character always grounded:
    Code (CSharp):
    1. grounded = Physics.Raycast(infantryControllerGO.bottomBound + transform.up * 0.1f, -transform.up, 0.25f);
    Code that works fine:
    Code (CSharp):
    1. grounded = Physics.Raycast(infantryControllerGO.bottomBound + transform.up * 0.1f, -transform.up, out RaycastHit hit, 0.25f);
    Screenshots of the 2 RaycastHit tooltips CLEARLY stating that parameter 3 is MaxDistance and not LAYER

    That's all for my rant

    I'd still like an explanation to this nonsense

    (My explanation is that there is one of the setup that doesn't use the maxDistance but the layer, but then WHY the tooltips are wrong??)

    EDIT: On Unity 2021.3.14f1 on MacOS
     

    Attached Files:

    Last edited: Jan 14, 2023
  2. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    147
    Just to be clear, I also tried it this way to be sure it wasn't a parameter issue, even though I'm sure it's not :)
    Code (CSharp):
    1. grounded = Physics.Raycast(origin : infantryControllerGO.bottomBound + transform.up * 0.1f,
    2.                                                                        direction : -transform.up,
    3.                                                                        maxDistance : 0.25f);
    While this works without any issues:
    Code (CSharp):
    1. grounded = Physics.Raycast(origin : infantryControllerGO.bottomBound + transform.up * 0.1f,
    2.                                                                        direction : -transform.up,
    3.                                                                        out _groundedHit,
    4.                                                                        maxDistance : 0.25f);
    Anyone from Unity would care to explain why the hell this system works the way it does?

    Those 2 codes give two different outcomes, entirely

    I'll repeat it one more time, first version returns true all the time, second works as it should
     
  3. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    446
    The code is correct. I tested this and it worked as expected (with and without RaycastHit).
    Code (CSharp):
    1. bool hitA = Physics.Raycast(origin, direction, distance);
    2. bool hitB= Physics.Raycast(origin, direction, out RaycastHit hitInfo, distance);
    3. print($"hit A= {hitA}, hit B= {hitB}");
    What version of Unity are you using?
     
  4. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    147
    Thank you very much for testing this!

    I am using Unity 2021.3.14f1 on MacOS