- Home /
Physics.SphereCast with Vector3.Reflect returns weird results
Hello, I try to have a ray that simulates the position of a ball which will move and collide with others objects. I use the Physics.SphereCast and Vertex3.Reflect method to do this but I got bad results, I added a screenshot, in red it's the calculated ray and in green as it should be.
Here is my script to show the ray :
// Create start ray
Ray ray = new Ray(objectWorldPosition, direction);
// Ray loop
RaycastHit hitInfo;
for (float length = 0.0f; length < 100.0f; length += hitInfo.distance) {
// If raycast hits something
if (Physics.SphereCast(ray, 0.5f, out hitInfo)) {
// Calculate center position of ball
Vector3 centerVector = hitInfo.point + (hitInfo.normal * 0.5f);
// Draw line
Debug.DrawLine(ray.origin, centerVector, Color.red);
// Get vector from start to hit point
Vector3 rayVector = centerVector - ray.origin;
// Calculate reflective ray vector
Vector3 reflectiveRayVector = Vector3.Reflect(rayVector, hitInfo.normal);
// Update ray
ray = new Ray(centerVector, reflectiveRayVector);
} else {
// Exit loop
break;
}
}
The green ray is calculated with a Physics.RayCast and a Pythagore formula to find the center point of the ball from the hitpoint. It is more accuracy but with some angles that fails too and also on some situations where ball could not pass but ray does because it is thinner.
Can someone help me ? Thanks
Well it depends. Because of the size of the ball, the side of the ball will hit the wall, where as the ray simulating the center of the ball isn't hitting? Just a guess. Because the ray simulates the center of the ball where as the ball itself hits the wall and not the center of it. Could also be because the ball is rotating or has other physics affecting its path. $$anonymous$$ake sure you have the radius set right too
With SphereCast, it finds the hitpoint for the side of the ball, I convert then the hitpoint to the position of the center by adding the normal vector of the collider multiplied by the radius of the ball. The radius is correct and the ball is constrained in x, y and z for rotation.
Answer by johnmph · Apr 14, 2014 at 04:17 PM
It is because Physics.SphereCast returns a wrong normal vector.
See http://answers.unity3d.com/questions/50846/how-do-i-obtain-the-surface-normal-for-a-point-on.html
Your answer
Follow this Question
Related Questions
Manuals Sphere collison 0 Answers
Raycast Never Hits Anything?! 2 Answers
SphereCast inaccurate? 0 Answers
Raycast // Accuracy math pls help 1 Answer