Module 1: Introduction to Game Development
Module 2: Unity Interface and Basics
Module 3: Introduction to C# Programming for Unity
Module 4: Physics and Movement
Module 5: 2D Game Development
Module 6: 3D Game Development
Module 7: User Interfaces and Game Mechanics
Module 8: Animation and Visual Effects
Module 9: Sound Design and Implementation
Module 10: Building and Deploying Your Game
Module 11: Advanced Topics and Next Steps

3.6 Introducing Gravity with Rigidbody

To make movement more realistic, let’s add physics.

Step 1: Add a Rigidbody Component

  1. Select the cube in the Hierarchy.
  2. In the Inspector, click Add Component and choose Rigidbody.

Step 2: Update the Script
Modify the script to use physics-based movement:

void FixedUpdate() {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
    GetComponent<Rigidbody>().AddForce(movement * 10);
}

Explanation:

  • FixedUpdate(): Used for physics-related calculations.
  • AddForce: Applies force to the Rigidbody, moving the object.

Step 3: Test It

  1. Save the script.
  2. Press Play and observe the cube’s physics-based movement.