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.6 Adding Forces for Realistic Interactions

You can apply directional forces to create effects like pushing or pulling objects.

Example: Pushing an Object
Create a new script called PushObject and attach it to the player:

public float pushForce = 10f;

void OnCollisionEnter(Collision collision) {
    Rigidbody otherRb = collision.gameObject.GetComponent<Rigidbody>();
    if (otherRb != null) {
        Vector3 pushDirection = collision.contacts[0].normal * -1;
        otherRb.AddForce(pushDirection * pushForce, ForceMode.Impulse);
    }
}

Explanation:

  • Checks if the collided object has a Rigidbody.
  • Calculates the direction of the force based on the collision point.
  • Applies an impulse to simulate a push.

Test It: Add a Rigidbody to another object, collide with it, and watch it move.