Search Unity

Physics.Raycast - Hit.point confusion

Discussion in 'Physics' started by KarlKarl2000, Mar 27, 2022.

  1. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    EDIT:
    Simpler solution
    https://answers.unity.com/questions/384355/stopping-a-raycast-after-hit.html
    EDIT:





    Hi guys,

    I need some help. :(

    https://docs.unity3d.com/ScriptReference/RaycastHit-point.html

    I'm trying to understand
    hit.point
    ... but something is not clicking for me. Been at this for hours.

    The scenario, 2d platformer, enemy needs to know if the hero is in FRONT or BEHIND a wall.

    When I run the code below, the output for the
     _rayHit.point
    value is IDENTICAL on both the hero and wall. :oops:

    What is expected -- A difference between the hero and wall values. IE: If the hero is closer to enemy, the vector3 value should be lower than the wall

    Code (CSharp):
    1.         if (Physics.Raycast(_rayForward.origin, _rayForward.direction, 10, _heroInt))
    2.         {
    3.             if (Physics.Raycast(_rayForward, out _rayHit, 20))
    4.             {
    5.                 Debug.DrawLine(_rayForward.origin, _rayHit.point, Color.green);
    6.                 print("hero point " + _rayHit.point);
    7.             }
    8.         }
    9.  
    10.         if (Physics.Raycast(_rayForward.origin, _rayForward.direction, 10, _wallInt))
    11.         {
    12.             if (Physics.Raycast(_rayForward, out _rayHit, 20))
    13.             {
    14.                 Debug.DrawLine(_rayForward.origin, _rayHit.point, Color.red);
    15.                 print("WALL point " + _rayHit.point);
    16.             }
    17.         }
    I also tried hit.distance and that also gave me identical values for both hero and wall

    Thanks for the help
     
    Last edited: Mar 27, 2022