- Home /
Raycasting ray go through object with gravity enabled
Hello,
I have an issue with Physics.Raycast when trying to detect the hit with some objects. The hit is detected at first and then seems to go deep in the object and finally go through it (it hits the floor behind it). You can see the behavior on the gif below:
(debug.ray in yellow and red gizmos spheres at hit point - black at beggining due to gif conversion, sorry for that)
I have Navmesh agent and Rigidbody components on the cylinder and after few debug tests, I realized that I hadn't the issue if I unchecked "Use Gravity" on the Rigidbody component. It also works when keeping "Use Gravity" and enabling "Is Kinematic" (and that probably be the solution I will use ) or enabling "Freeze position: Y".
Do you know what is happening?
Do you think it is an Unity issue?
I don't think my code as anything special but in case there's something I didn't see, here it is:
foreach (GameObject t in targets)
{
Vector3 rayDirection = (t.transform.position - transform.position);
rayDirection.Normalize();
Debug.DrawRay(transform.position, rayDirection * 100f, Color.yellow);
Debug.Log(gameObject.name + " can see " + t.name + ": " + Physics.Raycast(transform.position, rayDirection, out RaycastHit raycastHit, Mathf.Infinity, LayerMask.GetMask(new string[] { "Soldiers", "Buildings" })));
}
(Same thing without the LayerMasks parameters for the sphere gizmos)
Your answer
Follow this Question
Related Questions
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers
Physics gravity appears very weak 2 Answers
Object goes through wall. 0 Answers
turning off gravity for a controller 3 Answers
Increase Gravity? 2 Answers