- Home /
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.
Answer by Chronos-L · Mar 03, 2013 at 05:26 AM
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 )
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Scoring not working for pong 1 Answer
How to open a door after a numerical password? 1 Answer
Pong AI/Paddle controller help 1 Answer
Simple Collision Deflection (Pong) 1 Answer