If you are wanting to retrieve the point of contact for a collision between the Ball and the Paddle you can use the following code;
/// <summary>
/// Handles the OnCollisionEnter2D event triggered by an incoming collider making contact with the Ball's collider
/// </summary>
/// <param name="collision">The Collision2D data associated with this collision</param>
private void OnCollisionEnter2D(Collision2D collision)
{
foreach (ContactPoint2D impact in collision.contacts)
{
if (collision.gameObject.name == "Paddle")
{
Vector3 paddleLocalImpactPoint = _paddle.transform.InverseTransformPoint(impact.point);
Debug.Log("Paddle Impact\n\n" +
"World: X = " + impact.point.x.ToString() + " Y = " + impact.point.y.ToString() + "\n" +
"Local: X = " + paddleLocalImpactPoint.x + " Y = " + paddleLocalImpactPoint.y + "\n" +
"\n-----------------------\n");
}
}
In the above example, we specifically only look for a collision with the Paddle, however, you could potentially check for any other GameObject also (a block for example).
-
impact
is a ContactPoint2D and provides details about a specific point of contact involved in a 2D physics collision -
point
is a Vector2D and is the point of contact between 2 colliders in world space
Whilst retrieving the point of collision in the world space may be useful, it can also be useful to retrieve the point of collision local to the incoming collider (the Paddle in this scenario).
-
InverseTransformPoint
transforms the position from world space to local space
With a centralised pivot on your Paddle GameObject, a negative x value would indicate the impact occurred on the left hand side, a positive x value would indicate the impact occurred on the right hand side.
Note: The console only displays 2 line of information so the above is formatted in such a way that you click on the item and see the full information displayed in the expand section of the Console window, see below;