Can anyone give me some quick advice on how to fix a few things with Pong.

Hello, I am relatively new to C# and Unity. I have almost managed to complete Pong written in C# , I have implemented a scoring system, AI, (almost) realistic physics for the ball and out of bound areas. But there are two minor issues that I am having trouble with.

The first is the ball, I have coded it so that it bounces off of both rackets perfectly. But when it comes into contact with the north and south boundaries it will just simply glide along on the surface of the boundary and not actually bounce.

else if(other.gameObject.name == "TopBorder")
		{
			rigidbody.velocity = new Vector3(rigidbody.velocity.x,
			                                 rigidbody.velocity.y,
			                                 rigidbody.velocity.z * -1.0f);
		}
		else if(other.gameObject.name == "BottomBorder")
		{
			rigidbody.velocity = new Vector3(rigidbody.velocity.x,
			                                 rigidbody.velocity.y,
			                                 rigidbody.velocity.z * 1.0f);
		}

With these two bits of code I am trying to make the ball bounce off of the border but it follows the x axis instead of the z and I am not sure why.

Also when the ball goes out of bounds how do I reset it to it’s original position without reloading the scene to keep the player/enemy score?

Thank you in advance!

Hey,

I actually had a similar problem as far as the ball is concerned when I made a Brick Attack style game. If I remember right I just made it so the ball could never travel perfectly along that axis. It always had to have a diagonal direction at least slightly. I might have solved it all together but I’ll have to find that code. But basically I just made it alter the direction a bit if it was travelling along the wall.

As far as the bounds, there are a few ways to do this. You could get 2 Vector3s and make sure that the ball is always in the middle of them. Or you can simply make a trigger that resets the ball.

For the borders why not give them a bouncy physics material.

Why have you got a velocity on all 3 components, I thought pong was a 2D game so one of the axis velocity should be 0 ?

As for going out of bounds you could have 2 colliders just outside the play area and when the ball hits a collider you jus set its position and velocity to whatever you want