How do you use Contactpoint to find out where on a gameobject another object hit?

Hey all,

Im working on a pong-like game and I’m trying to control how the ball bounces of the paddle. The way I want it set up is depending on what part of the paddle is hit, the ball is returned at a certain angle. Right now I am using contactpoints to get it done, but contactpoints returns where the collision happened in the world space. I want to know where it happened on the paddle. Is there a way to get the position on that paddle where it collided? Here is my OnCollisionEnter code:

`if (col.collider.CompareTag(“Paddle1”) || col.collider.CompareTag(“Paddle2”))
{
audio.Play();
WhoHit(col);

		foreach( ContactPoint contact in col.contacts)
	    {
            float normContactY = contact.point.y;

			if(normContactY < 4f && normContactY > 3f)
				normContactY = 4f;
			else if(normContactY < 3f && normContactY > 2f)
				normContactY = 3f;
			else if(normContactY < 2f && normContactY > 1f)
				normContactY = 2f;
			else if(normContactY < 1f && normContactY > 0f)
				normContactY = 1f;
			else if(normContactY < 0f && normContactY > -1f)
				normContactY = -1f;
			else if(normContactY < -1f && normContactY > -2f)
				normContactY = -2f;
			else if(normContactY < -2f && normContactY > -3f)
				normContactY = -3f;
			else if(normContactY < -3f && normContactY > -4f)
				normContactY = -4f;

			float newAngle;

			if(normContactY  > 0)
            	newAngle = (((normContactY * 2) / col.collider.bounds.size.y) * bounceAngle) + minBounceAngle;
			else
				newAngle = (((normContactY * 2) / col.collider.bounds.size.y) * bounceAngle) - minBounceAngle;
           
			rigidbody.velocity = new Vector3(Mathf.Cos(newAngle), Mathf.Sin(newAngle), 0) * rigidbody.velocity.magnitude;
            
			Debug.Log(" Angle = " + newAngle);
	    }
    }`

minBounceAngle is the widest we want the ball to bounce off the paddle.

BounceAngle contains the sum of all the intervals of angles we want to use.

Thank you for any help.

If you want to convert a position from world space into the local space of a particular transform (like your paddle), you can use Transform.InverseTransformPoint, like so:

Vector3 worldPosition = new Vector3(0, 0, 0);
Vector3 localPosition = paddleTransform.InverseTransformPoint(worldPosition);

This will return the local position relative to the paddle’s pivot point. Therefore, it’s important that your paddle’s pivot point is strategically placed to give you the most valuable data. For example, if you place the pivot in the center of the paddle, you might be able to expect that negative X values are the left side of the paddle and positive X values are the right side.