# Raycast detecting terrain at weird position

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

1. ### SparrowsNest

Joined:
Apr 6, 2017
Posts:
267
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.             if (Physics.Raycast (arc [i - 1], arc [i] - arc [i - 1], out hit)) {
10.                 Debug.DrawRay (arc [i - 1], arc [i] - arc [i - 1], Color.red, 10f);
11.                 //return false;
12.                 //Debug.Log (arc [i - 1] + "<|>" + (arc [i] - arc [i - 1]));
13.                 Debug.Log (hit.collider.transform.name);
14.                 s = false;
15.             } else {
16.                 Debug.DrawRay (arc [i - 1], arc [i] - arc [i - 1], Color.green, 10f);
17.             }
18.         }
19.
20.         /*
21.         for (int i = 0; i < arc.Length - 1; i++) {
22.             RaycastHit hit;
23.             if (Physics.Raycast (arc [i], arc [i + 1] - arc [i], out hit)) {
24.                 Debug.DrawRay (arc [i], arc [i + 1] - arc [i], Color.red, 10f);
25.                 //Debug.Log (arc [i - 1] + "<|>" + (arc [i] - arc [i - 1]));
26.                 //Debug.Log ("False "+ hit.collider.transform.name);
27.                 s = false;
28.             } else {
29.                 Debug.DrawRay (arc [i], arc [i + 1] - arc [i], Color.green, 10f);
30.             }
31.         }
32.         */
33.         Debug.Log (s.ToString());
34.         return s;
35.     }

File size:
573.9 KB
Views:
48
2. ### SparrowsNest

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