I am making a Brick Breaker game, and I want to know how do I make the ball reflect based on where it his the pad?
In Unity, is there a way to change how the ball reflects off the pad based on where it hits the pad, for example, hitting the edge of the pad will reflect a lot in the x direction, but hitting the ball to the center hardly reflects the ball’s x direction?
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Pad")
{
float difference = transform.position.x - col.gameObject.transform.position.x;
TempvelocitySpeedY = GetComponent<Rigidbody2D> ().velocity.y;
if (TempvelocitySpeedY <= 0f)
{
TempvelocitySpeedY *= -1f;
}
if (difference < 0f)
{
difference = Mathf.Clamp (difference, -0.35f, -0.1f);
}
if (difference > 0f)
{
difference = Mathf.Clamp (difference, 0.1f, 0.35f);
}
GetComponent<Rigidbody2D> ().velocity = Vector2.zero;
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (bouncespeed * difference, TempvelocitySpeedY));
GetComponent<Rigidbody2D> ().velocity = new Vector2 (velocitySpeedXPad * difference * 3f, TempvelocitySpeedY);
print (GetComponent<Rigidbody2D> ().velocity.x);
Here is my code for the ball. I was wondering if you could please suggest me an edit on how can I change the direction of the ball depending where it hits the pad, the more towards the edge, the more the ball will reflect diagnoly
Your answer

Follow this Question
Related Questions
Animation clip only plays once when button is pressed, and then will not play again 2 Answers
My game script is fine but I'm still getting unexpected class error 0 Answers
Convert Keyboard controlls to UI Buttons 1 Answer
How do i calculate in which direction my character is moving relative to it's rotation? 0 Answers
Problem with animation script. 1 Answer