3D pong bounce direction help

I’m currently trying to mess around with a little 3D pong game but I’m having difficulty getting the ball to bounce off based on where it hits the paddle, it’s always just bouncing in one direction regardless of where it hits on the paddle (see here Screen capture - 96f0327b1be312d816d5061cec5417a1 - Gyazo)

The script controlling it is here

float hitFactor(Vector3 ballPos, Vector3 racketPos, float racketHeight)
{
return (ballPos.x - racketPos.x / racketHeight);
}

void OnCollisionEnter(Collision col)
{
if(col.gameObject.name==“P1Paddle”)
{
float x = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.x);
Vector3 dir = new Vector3(1, x).normalized;
Vector2 newdir = dir * speed;
rb.velocity = new Vector3(newdir.x,newdir.y,rb.velocity.z);
}
if (col.gameObject.name == “P2Paddle”)
{
float x = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.x);
Vector3 dir = new Vector3(-1, x).normalized;
Vector2 newdir = dir * speed;
rb.velocity = new Vector3(newdir.x, newdir.y, rb.velocity.z);
}
}

I’m assuming my maths is completely wrong but I’m completely stuck with where to go now, sorry if this is a stupid question.

Couple of things: first, use code tags so your code is readable in here.

Second of all, google up left hand rule and then look at the little 3D widget in the Unity3D editor’s scene window: X is red, Y is green, Z is blue… the colored cone is the positive direction in each three actions.

You can hold up your left hand with your thumb pointing right, your index pointing up, your palm facing away: that corresponds to X+ is thumb, Y+ is index up, Z+ is palm forward. (edited)

It is an awesomely powerful tool for solving these sorts of problems.

Now… I think some of your x, y, and z calculations are mixed up. I think you are fundamentally on the right track but I can’t easily tell without code formatting. I recommend learning left hand rule “cold” so you can use it to just go over your different calculations. Do you really want the difference in X to be the paddle offset? Because then you’re sticking it into Y. Do you really want the new velocity to be based off the new inputs and the Z portion of the old input? Maybe, but I suspect not.

These are some of the things you need to answer for yourself, but they are completely dependent on how you laid out your levels: up/down? fore/aft? left/right? Assuming what I see in your GIF, it appears you always want Y velocity to be zero, and only vary X and Z. This does not appear to be what your code is doing.

Hope this give you some pointers.

Thank you those are really helpful, I’m just at a loss I think for how to tackle this now, I think I may have bitten more than I can chew. Thank you for the useful tips though! Hopefully I’ll return to this project and get it working in the future!

Oop, I corrected my post above: +Z is palm forward.

Don’t get discouraged… with left hand rule you have a very powerful mental model to think in the problem space, so work on one piece at a time and gain confidence with the model and how to use it. Like for example, you have it working to control the paddles… verify how all that works with left hand rule so you get confidence.