Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast detecting terrain at weird position

Discussion in 'Physics' started by SparrowGS, May 13, 2018.

  1. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    So I'm working on some ballistics, trying to trace a path with raycasts when the terrain is being detected where there is none.

    Didn't post a bug report yet because this is in a big project and i wanna make sure i'm not on the wrong here.

    below is the code for tracing the path.
    note, there is two loops, one is commented out, they both give the same result.
    Code (CSharp):
    1.     public static bool CheckBallisticPath(Vector3 startPos, Vector3 forward, float velocity, float maxTime, float timeResolution){
    2.        
    3.         Vector3[] arc = GetBallisticPath (startPos, forward, velocity, maxTime, timeResolution);//getParabolaArc (start, end, velocity, low);
    4.         //Debug.Log (arc.Length);
    5.         //Debug.Log (arc [arc.Length - 1]);
    6.         bool s = true;
    7.  
    8.         for (int i = 1; i < arc.Length; i++) {
    9.             RaycastHit hit;
    10.             if (Physics.Raycast (arc [i - 1], arc [i] - arc [i - 1], out hit)) {
    11.                 Debug.DrawRay (arc [i - 1], arc [i] - arc [i - 1], Color.red, 10f);
    12.                 //return false;
    13.                 //Debug.Log (arc [i - 1] + "<|>" + (arc [i] - arc [i - 1]));
    14.                 Debug.Log (hit.collider.transform.name);
    15.                 s = false;
    16.             } else {
    17.                 Debug.DrawRay (arc [i - 1], arc [i] - arc [i - 1], Color.green, 10f);
    18.             }
    19.         }
    20.  
    21.         /*
    22.         for (int i = 0; i < arc.Length - 1; i++) {
    23.             RaycastHit hit;
    24.             if (Physics.Raycast (arc [i], arc [i + 1] - arc [i], out hit)) {
    25.                 Debug.DrawRay (arc [i], arc [i + 1] - arc [i], Color.red, 10f);
    26.                 //Debug.Log (arc [i - 1] + "<|>" + (arc [i] - arc [i - 1]));
    27.                 //Debug.Log ("False "+ hit.collider.transform.name);
    28.                 s = false;
    29.             } else {
    30.                 Debug.DrawRay (arc [i], arc [i + 1] - arc [i], Color.green, 10f);
    31.             }
    32.         }
    33.         */
    34.         Debug.Log (s.ToString());
    35.         return s;
    36.     }
     

    Attached Files:

  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Solved it, it was raycasting in the direction for ∞.
    made it use the direction.magnitude.

    I thought if the max distance isn't specified it'll use the direction.magnitude by itself.