- Home /
Ball stuck horizontally between rackets in pong
Hello everyone! I'm writing a Pong Game and sometimes after I reset the ball position (after a point has been scored) (to reset the ball position I just transfer the ball in the middle) the ball goes horizontally forever. Any solutions? I tried this but it doesn't work:
function Update () {
if (rigidbody2D.velocity.x==0){
rigidbody2D.velocity.x=-4.8;
}
if (rigidbody2D.velocity.y==0){
rigidbody2D.velocity.y=-4.8;
}
}
How do you reset the ball?
You can reset the ball's velocity directly:
rigidbody2D.velocity = Vector2.zero;
I reset the ball with this: Is there a way to reset it with a specific angle(or another solution)? function Update () { //Check right bounds if (transform.position.x > 7){ leftScore++;
transform.position.x=0;
transform.position.y=0;
//rigidbody2D.velocity.y=0;
}
//Check left bounds
if (transform.position.x < -7){
rightScore++;
transform.position.x=0;
transform.position.y=0;
//rigidbody2D.velocity.y=0;
}
}
Hmm.
If I had to guess, you might be in a circumstance where the ball's velocity is very small (say, 0.0001
meters per second), but not quite exactly zero. Computers are famously fussy about that.
Rather than checking if velocity is exactly zero, you might check if it's very close:
if ($$anonymous$$athf.Abs(rigidbody2D.velocity.x) < 0.1)