HI, I’m pretty new to Unity and coding and i have this problem while coding an effect of bouncing a text object off the wall (screen boundary ).
Kind of like the display on the DVD player when not used, just kept bouncing around.
But he bug here is that it sometimes gets out of the bound that I set and just stopped there, or sometimes went quivering and continued in an irrational direction.
Here is my code, it’s the simplest way i could think of to do it
transform.Translate(velocity*Time.deltaTime);
if (transform.position.x >= GetBoundary._MAX_X || transform.position.x <= GetBoundary._MIN_X)
{
velocity = Vector2.Reflect(velocity, new Vector2(1, 0));
velocity *= 0.8f;
}
if (transform.position.y >= GetBoundary._MAX_Y || transform.position.y <= GetBoundary._MIN_Y)
{
velocity = Vector2.Reflect(velocity, new Vector2(0, 1));
velocity *= 0.8f;
}
Thanks a lot for answering.