Simple sample of a 2D ball bouncing in Unity. Steps taken (no code required):
- Create a "Sprites" folder underneath the Assets folder.
- Import a ball and ground image into the "Sprites" folder and set them to be single sprites - Texture Type: Sprite (2D and UI), Sprite Mode: Single.
- Create a new empty object under the Hierarchy and name it Ball. Add a Sprite Renderer component to the object and assign it the ball sprite.
- Create a "Materials" folder underneath the Assets folder.
- Create a new Physics2D Material in the "Materials" folder and label it "Bouncy". Set the Friction to 0.2 and the Bounciness to 0.8.
- Add a Circle Collider 2D to the Ball object and assign it the Bouncy material.
- Add a Rigidbody 2D component to the Ball object (so gravity is applied to the object).
- Create a new empty object under the Hierarchy and name it Ground. Add a Sprite Renderer component to the object and assign it the ground sprite.
- Add a Box Collider 2D component to the Ground object.
- Ensure that the Ball object is positioned above the Ground object. Start the scene and the ball object should drop and bounce off the ground.
Tutorial exercise taken from https://unity3d.com/learn/tutorials/modules/beginner/scripting/simple-clock