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.