Can Someone explain why the racast is only hitting the on the first two collisions.
The issue i am having is that when I attempt to use a ray to check for colliders I am only hitting the first two times. the test environment is set up with a generic plane, and two cubes on top of the plane. These objects are placed in a straight line on the x-axis. The first two rays hit the plane an report the correct information and translate the ray to the appropriate position. The third ray misses the plane. removing the fail checks runs the loop and translates the ray all 100 times successfully yet fails to hit after the first two loops anyway. scaling the plane will cause the ray check to succeed up to 7 times.
What is causing these failures?
RaycastHit Hit;
public Vector3 RayPosition;
public Vector3 Rayendpoint;
public float raylength;
public bool TestDone;
int Fails;
void Start()
{
RayPosition = new Vector3(0, 10, 0);
TestDone = false;
}
void FixedUpdate()
{
if (TestDone == false)
{
RayChecker();
}
}
void RayChecker()
{
for (int i = 0; i < 100f; i++)
{
int MaxFails = 10;
Rayendpoint = new Vector3(RayPosition.x, RayPosition.y - raylength, RayPosition.z);
Debug.DrawLine(RayPosition, Rayendpoint, Color.red, 360f, false);
if (Physics.Raycast(RayPosition, Rayendpoint, out Hit, 360f))
{
Debug.Log(Hit.collider.name);
Debug.DrawLine(RayPosition, Rayendpoint, Color.yellow, 360f, false);
Debug.Log(RayPosition + " : " + Rayendpoint);
RayPosition.x++;
}
else if (Fails == MaxFails)
{
break;
}
else
{
Debug.Log("FAil");
Fails++;
}
{
}
}
Debug.Log("LOOPDONE");
TestDone = true;
}
Your debug lines have 360f distance but your Raycast has only 15f. You should also Debug.Log your RayPosition.x
The 360f is actually a duration in seconds for the DrawLine method. all of the lines are drawn form a start point to an end point so changing the max distance won't actually change much. Also, the debug logs the Vector3 so it returns the X,Y,and Z coordinates.
Ha my bad :P For me to assume to u made an ass outta me ;) If you have a more completed script I'll gladly try it. In your above snippet RayPosition is undefined so its hard to see whats going on.
Answer by nrocpop · Apr 04, 2018 at 03:50 AM
I just realized that i was attempting to define an endpoint(Such as the one called for in the DrawLine method) instead of a direction. By replacing Physics.Raycast(RayPosition, Rayendpoint, out Hit, 360f) with Physics.Raycast(RayPosition, Vector3.down, out Hit, 15f) the ray is hitting as expected. My issue has been resolved.