- Home /
Kinematic Rigidbody collision detection
Hi all
I'm trying to implement some very basic collision detection for a simple game. For example: when a ball hits a block I want to calculate the normal of the collision and reflect the ball on that normal.
I've gone through the routine of setting up colliders to find that they don't really do anything unless you setup a RigidBody component. From there I discovered that I needed to set the RigidBodies to Kinematic in order for physics to stop interfering.
At this stage I have an OnTriggerEnter method that gets called to tell me that a Collider is present, but this doesn't tell me anything about the actual collision.
Furthermore, as I understand it, OnCollisionEnter is only called on non-kinematic RigidBodies.
tl;dr:
How do I get collision information without RigidBody physics simulation?
Thanks, Ves
Could you use physics.raycast, get the hit.point where the ray hit the collider in world space and calc normal from that?
You could probably freeze rotation and movement along all axes, leave is$$anonymous$$inematic set to false, and move it via the transform component (although it's just a hunch that I've not tested).
Answer by VesuvianPrime · Jan 19, 2014 at 02:54 AM
As suggested by getyour411 I was able to implement some raycasting between the objects in the collision. From here I could calculate the reflection vector and use that to adjust velocity:
// Causes the ball to "bounce" against the given collider
public void reflect(Collider other)
{
// We do a raycast to figure out the normal of the collision
RaycastHit hitInfo = new RaycastHit();
Vector3 direction = other.bounds.center - this.collider.bounds.center;
if (!Physics.Raycast(this.collider.bounds.center, direction, out hitInfo))
throw new Exception("Couldn't calculate normal");
// Now we can calculate the reflection
Vector3 velocity3 = new Vector3(this.__velocity.x, this.__velocity.y, 0);
Vector3 reflection = Vector3.Reflect(velocity3, hitInfo.normal);
this.velocity = new Vector2(reflection.x, reflection.y);
}