I need to figure out good pong ball physics

I feel like this should be alot simpler than it actually is. But I’ve been trying to make good physics for the ball in
my pong game. I want the ball’s trajectory to change depending on where it hits the paddle. I’ve thought of all I can, and looked everywhere online, but no luck…

Does anybody have an idea on how to achieve this effect? I’d like to do it through C# code if possible.
Any help would be great!

Use Vector3.Reflect(ball direction, normal of the surface that the ball has hit) this will return a new direction based on where it hits the paddle Unity - Scripting API: Vector3.Reflect

Can you just add a physics material with the bounce turned up?

The man has a point there. I made a Pong type game before. I found the best way to handle physics (for me), was to create everything in 2D physics, and give each object a different physics material. So you could change it’s bounce effect against different object types. However @Aidenjl 's idea would suit if you find you’re already committed to the scripting of it route.

Thanks for your help everybody! :slight_smile: I ended up going with @Aidenjl 's suggestion and use Vector3.reflect. With a little
work I was able to implement it in my game and got my ball bouncing to my liking.

3 Likes

Unity says the name isRight does not exist in the current context it has to do with this code
// if hitting left paddle and moving right, flip direction
if (isRight == false && direction.x < 0)
{
direction.x = -direction.x;
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
[SerializeField]
float speed;

float radius;
Vector2 direction;

// Start is called before the first frame update
void Start()
{
direction = Vector2.one.normalized; // direction is (1,1) normalized
radius = transform.localScale.x / 2; // half the width
}

// Update is called once per frame
void Update()
{ //Bounce off top and bottom
transform.Translate(direction * speed * Time.deltaTime);
if (transform.position.y < GameManager.bottomLeft.y + radius && direction.y < 0)
{
direction.y = -direction.y;
}

transform.Translate(direction * speed * Time.deltaTime);
if (transform.position.y > GameManager.topRight.y - radius && direction.y > 0)
{
direction.y = -direction.y;
}
// game over
if (transform.position.x < GameManager.bottomLeft.x + radius && direction.x < 0) ;
{

}
if (transform.position.x > GameManager.topRight.x - radius && direction.x > 0) ;
{

}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == “Paddle”)
{
bool isRight = other.GetComponent ().isRight;
// if hittingRightpaddle and moving right, flip direction
if (isRight == true && direction.x > 0)
{
direction.x = -direction.x;
}
}

// if hitting left paddle and moving right, flip direction
if (isRight == false && direction.x < 0)
{
direction.x = -direction.x;
}

}

}