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.4 Implementing Gravity and Fall Detection

Unity applies gravity automatically to Rigidbodies. Let’s expand on this to detect when an object falls off the playable area.

Step 1: Create a Fall Detection Script
Attach the following script to your player object:

void Update() {
    if (transform.position.y < -10) {
        Debug.Log("Player has fallen!");
    }
}

Explanation:

  • The script checks the player’s vertical position.
  • If the position drops below a certain threshold, it triggers a message (or can reset the position).

Step 2: Reset Player Position
Modify the script to reset the player’s position:

void Update() {
    if (transform.position.y < -10) {
        transform.position = new Vector3(0, 2, 0);  // Reset to starting point
    }
}

Test It: Move your object off the platform and watch it reset when it falls.