Hello, as you can you see I need help with pong. A bit sad, but I must overcome this thing. I know how to get the paddles moving and everything, but I can’t figure out the physics and scripting for how to get the ball to move. I would really much like your help, and opinions. Here is a screen shot.
I’m a total newbie in Unity, since I am just learning the basics myself at the moment.
Fortunately (for you) Pong happened to be my first Project.
Currently I’m learning UnityGUI to do a Main and a Pause menu. The game itself is finished.
I’ll give you some tips so you can solve your problems.
The most important thought you must have is: “How have they solved it on those old computers back then?”
The thing is that you don’t need the complex physics calculations of Unity, since the Ball with always react in a simple manner.
- For Pong it’s recommended to store the velocity of the Ball in a variable.
- Use the Update() method to set the Ball to the specified speed on each frame (so it doesn’t slow down)
- Depending on what plane you use (xy, xz, yz) you need to negate only one of the components of the velocity whenever the Ball collides with something (Hint: OnCollisionEnter())
Try to do it yourself, if you need further help, here’s a code snippet:
Ball.cs
public class Ball : MonoBehaviour {
private Vector3 v = new Vector3(10.0f, 0.0f, 0.0f);
void Update() {
rigidbody.velocity = v;
}
void OnCollisionEnter(Collision collision) {
if (collision.collider.name == "Player" || collision.collider.name == "Opponent")
v.x = -v.x;
else if (collision.collider.name == "Wall_North" || collision.collider.name == "Wall_South")
v.z = -v.z;
}
}
It probably won’t work for you in that way, so don’t even bother copy pasting.