- Home /
Stop object velocity from moving horizontally
I have multiple game objects that bounce around the screen (similar to breakout). Every once in a while the objects start bouncing horizontally non-stop. Is there a way for me to detect when this happens or disable this type of movement completely?
Answer by FortisVenaliter · Apr 11, 2017 at 09:24 PM
When it collides with the edge and bounces, you can check to see if the y-velocity is close to zero, and if so, either destroy it, or just add a little random value to the y-velocity. That way it will get itself unstuck eventually.
Adding this in my Update() seems to work
if (rb.velocity.x > 0 && rb.velocity.y == 0) {
rb.velocity = new Vector2(
rb.velocity.x, speed
);
}
Your answer
Follow this Question
Related Questions
Why is enemy jumping at different speed even though the velocity is the same? 0 Answers
how to make an 2d object move the direction its facing using rigidbody2d.velcoity 1 Answer
how to jump at fixed height but faster 0 Answers
.normalized sometimes returns 0 values, in a condition that only functions if value is not 0... 1 Answer
rigidbody2D force or velocity? 2 Answers