Get inpact location OnCollisionEnter? (c#)

So currently I’m mostly derping around in unity trying to learn stuff and I’m making a “brick buster” type game (or what it’s called, the one where you control a platform to make a ball bounce on to it and destroy briks).

Right now I want to make the ball to the side depending on where on the platform it hits, so if it hits the corner it bounces way to the side but when it hits the middle it bounces straight up. I could do this with triggers on the platform but I also just want to check if it’s possible to get the position of where the ball collided with the platform and then turn that information into speed in a direction.

Sorry if my english is pretty bad, hope you understand :confused:

Thanks in advance.


OnCollisionEnter() has a ‘Collision’ parameter. From the Collision parameter you can access the ‘contacts’ array, which contains the location of all the contacts from the collision. For what you are doing, you can probably just process the first entry in the array. See explosion example here:

As for speed and direction, using contact may not be the way to approach the problem. Assuming you are using the physics engine (i.e. Rigidbody) for ball movement, then the easiest solution would be to check/track the Rigidbody.velocity of the ball. If you are not using physics, then just keep track of the position from the previous frame. The velocity of the object would then be:

Vector3 velocity = transform.position - prevPosition;

Speed is just the magnitude of the velocity.