I’m creating a game with a mechanic similar to Peggle. I’m launching a small spherical projectile at a capsule. The rigidbodies of the objects are constrained such that the collision is effectively 2D (just two circles colliding). I want to calculate the direction the projectile will be heading after the collision.
Currently this is my process:
- Call
Physics.SphereCast
along the path of the projectile. - Reflect the projectile velocity about the
RaycastHit.normal
vector. - Place an arrow object at
RaycastHit.point
facing the direction of the reflection vector.
Here’s the relevant code (the projectile is launched from the origin):
var raycastHit:RaycastHit;
if(Physics.SphereCast(Vector3.zero, radius, projectileDirection, raycastHit, range)) {
arrow.position = raycastHit.point;
arrow.LookAt(raycastHit.point + Vector3.Reflect(projectileDirection, raycastHit.normal), Vector3.up);
}
With this method, I’m having two problems:
- The arrow direction is not accurate (as much as 15 degrees off).
-
Physics.SphereCast
detects collisions that the actual projectile never hits (the radius of the sphere collider and the radius parameter passed toSphereCast
are the same).
I have a hunch that this means the actual physics calculation is done differently than I am doing it here (which is the typical “reflect the velocity vector around the contact normal” model). What I need to know is: how can I more accurately predict what the result of the collision is going to be, preferably without writing physics code myself?