# Realistic Bouncing Effect

I have a 2D project and am trying to make a nice smooth bouncing effect. I have a ball surrounded by walls and every 3 seconds adds a force to it and I also have a 2D physics material that is bouncy. But it doesn’t look natural and smooth at all? The code below is what I am doing now to get this effect. Any suggestions on how to do this properly?

Code

``````function FixedUpdate ()
{
}

{
yield WaitForSeconds(3);
yield WaitForSeconds(3);
yield WaitForSeconds(3);
yield WaitForSeconds(3);
}
``````

Applying forces to rigidbodies should always be done during FixedUpdate() - if you’re using co-routines to achieve some specific time interval, you should yield return WaitForFixedUpdate() prior to applying a force.

Multiplying values by Time.deltaTime to achieve framerate-indepentent reactions is wise in every OTHER scenario. That is, calculations which DON’T take place during FixedUpdate(), which is framerate-independent by its nature.

To clarify the last point, according to Unity’s physics system, a unit is equivalent to one meter for all physics calculations.

@Jammer3000 Regarding multiplying values by Time.deltaTime:

This is done during any non-fixed-timestep loop like Update() or LateUpdate() to any value whose purpose is related to changing values over a specific interval of time. This is because each update or “frame” can take different amounts of time, and failing to multiply by Time.deltaTime will mean your variable will behave differently depending on the framerate. This is (almost) always bad.

If you were moving something during Update(), without rigidbodies or the physics system, you’d want to multiply the desired speed by Time.deltaTime like this…

``````somePosition.x += speed * Time.deltaTime;
``````

…to ensure the correct distance per second is achieved regardless of framerate. That goes for any value which needs to change at a specific rate-per-second in any update loop except FixedUpdate()

The important feature of FixedUpdate() is that the interval between each frame is always the same amount of time, regardless of framerate. Applying forces should be done in FixedUpdate because the physics simulation updates in tandem with FixedUpdate.

If you want an object to bounce off other colliders:

Create a physics material with a high bounce value, no friction value, making sure to set the bounce multiplier to “maximum” and friction multiplier to “minimum” then assign this material to your bouncy object’s collider.

Also ensure there’s no drag on the rigidbody component of this bouncing object.

Apply a force one time, (at least, not every frame) and it’ll go on bouncing for a good long while. (until floating point imprecision causes the velocity to decay)