In my pong game How do I prevent paddle from going threw walls?

I have a paddle and when i move it it goes threw walls.

You can use either (this is up to personal preferences):

Rigidbody + collider

You can setup colliders for your paddle and wall.

Pros :

  • Automated process, no scripting

Cons :

  • You will get a jerking motion when you are trying to go through the wall

Limit position via value clamping

Since this a pong game, you can use a simple calculation to limit the position of your paddle at the certain axis.

public class Control : MonoBehaviour {
   public Transform paddle;
   public float upperLimit, lowerLimit;

   void Update() {
      //Process the moving paddle here (your current code)

      //Finalize/Limit the paddle position (in this example, y axis)
      //  You can do this seperately, or you can incorporated
      //  this into the moving code
      Vector3 pos = paddle.position;
      pos.y = Mathf.Clamp( pos.y, lowerLimit, upperLimit );
      paddle.position = pos;


Pros :

  • No jerking motion ( the jerking motion is caused by the re-position of the gameObject when it is forced through another collider )
  • More control

Cons :

  • Need to be re-adjusted for different wall distance ( negligible )