Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    350
    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:
    350
    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.