- Home /
Weird Raycast behaviour
NOTE: after reading the question read the comments, lots of more info there.
I have a sphere whit collider over a plane, and with rigidbody attached, but not forces in use at this testing moment. There are also a cube with a static collider, the one blue, and some others cubes without colliders, the little greens. I cast one raycast from each of the greens towards the sphere, and some ones dont collide. In the picture the fails are the gray lines, the cyan is when the ray hit any collider.
Nothing is moving nor rotating. A full static scene.
Code that cast rays:
public bool BuscaEnlace(GameObject goA, GameObject goB, out Vector3 enlace)
{
enlace = new Vector3(0, 0, 0);
Vector3 puntoA = goA.transform.position;
Vector3 puntoB = goB.transform.position;
RaycastHit hit;
foreach (Vector3 espejo in noVisuales)
{
if (Physics.Raycast(espejo, puntoA - espejo, out hit))
{
Debug.DrawLine(espejo, hit.point, Color.cyan, Mathf.Infinity);
Debug.Log("Vista a " + goA.name);
if (hit.transform.gameObject.name == goA.name)
{
if (Physics.Raycast(espejo, puntoB - espejo, out hit))
{
if (hit.transform.gameObject.name == goB.name)
{
Debug.DrawLine(espejo, hit.point, Color.gray, Mathf.Infinity);
Debug.Log("Vista a " + goB.name);
enlace = espejo;
return true;
}
}
}
}
else
{
Debug.DrawRay(espejo, puntoA - espejo, Color.gray, Mathf.Infinity);
}
}
return false;
}
It first look for linear sight of the sphere and then for the same to the cube in the right, something like a mirror, but fails and i dont know why.
If i change the code to this:
public bool BuscaEnlace(GameObject goA, GameObject goB, out Vector3 enlace)
{
enlace = new Vector3(0, 0, 0);
Vector3 puntoA = goA.transform.position;
Vector3 puntoB = goB.transform.position;
RaycastHit hit;
foreach (Vector3 espejo in noVisuales)
{
if (Physics.Raycast(espejo, puntoA - espejo, out hit))
{
// Debug.DrawLine(espejo, hit.point, Color.cyan, Mathf.Infinity);
// Debug.Log("Vista a " + goA.name);
// if (hit.transform.gameObject.name == goA.name)
// {
// if (Physics.Raycast(espejo, puntoB - espejo, out hit))
// {
// if (hit.transform.gameObject.name == goB.name)
// {
// Debug.DrawLine(espejo, hit.point, Color.gray, Mathf.Infinity);
// Debug.Log("Vista a " + goB.name);
// enlace = espejo;
// return true;
// }
// }
// }
}
else
{
Debug.DrawRay(espejo, puntoA - espejo, Color.gray, Mathf.Infinity);
}
}
return false;
}
I get only the gray lines as result. So i think not is the code, is almost commented, but i always read that raycast dont fail...
¿Some help?
New clue, If I remove rigidbody raycast works well...
With rigidbody some rays works some others no, as seen in the previous images...
No forces applied. Only component added.
Tested variations, kinematic, gravity on/of, freeze x/y/z and completly freezed, none works, if rigidbody present some rays fail as seen before.
Without rigidbody attached image:
Now if I attach rigidbody AND ADD a "background" static collider as seen in image, also works... ¿why?
Without that new static collider some rays dont work, but the debug rays go to the center of sphere...
So if a remove rigidbody or add far collider detects sphere, but without that dont... can someone enlighten me, please?
New images with rigidbody attached (no forces applied), moving sphere before run. Gray lines are raycast returning false.
If i keep calling the function, eventually i get the expected behaviour with rigidbody and without far border collider, but... ¿why fails the first time always on some raycasts?
Test projectlink text
Run out of box and check fail in raycast.
If enable "BorderTest1", run successfully
If you remove rigidbody from "Test1" sphere, run successfully.
If change:
void Deambula() { ObtenObjetivoDebug(); ObtenRuta(); //InvokeRepeating("ObtenRuta", 0, 1); }
to:
void Deambula()
{
ObtenObjetivoDebug();
//ObtenRuta();
InvokeRepeating("ObtenRuta", 0, 1);
}
First fail, in one second success.
Now can disable test1 (and bordertest1) and do the same tests with test2 and bordertest2, in another start point.
Please help to understand that behaviour, why need borders?, why if i dont add border at the second call (InvokeRepeating) without changes in the scene success? what does the rigidbody have to do?