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

4.2 Controlling Movement with Rigidbody

Physics-based movement uses Rigidbody forces for realism. Let’s implement this with a jump mechanic.

Step 1: Set Up a Jump Script
Create a script called PlayerMovement and attach it to your cube (or another object). Add the following code:

public float jumpForce = 5f;  // Strength of the jump
private Rigidbody rb;

void Start() {
    rb = GetComponent<Rigidbody>();
}

void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    }
}

Explanation:

  • Input.GetKeyDown: Detects when the spacebar is pressed.
  • AddForce: Applies an upward force to simulate a jump.
  • ForceMode.Impulse: Applies the force instantly.

Step 2: Test It

  1. Save the script and press Play.
  2. Press the spacebar to make the object jump.