Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Raycast detecting terrain at weird position

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

  1. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    998
    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. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    998
    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.