Physics Interactions by VizRCA - 3

PhysicsAI

Using the built-in physics engine to make reactive behaviours and environments. You must have some basic experience of Unity and Scripting.

Unity 2017.4.19f1MIT LicenseUpdated 122 days agoCreated on March 20th, 2019
Go to source

Physics-Interactions

Using the built-in physics engine to make reactive behaviours and environments. You must have some basic experience of Unity and Scripting.

Overview

Physics

Physics

Language and Anatomy

Details

In game engines, there is no assumption that an object should be affected by physics — firstly because it requires a lot of processing power, and secondly because it simply doesn’t make sense. For example, in a 3D driving game, it makes sense for the cars to be under the influence of the physics engine, but not the track or surrounding objects, such as trees, walls, and so on — they simply don’t need to be. For this reason, when making games, a Rigid Body component is given to any object you want under the control of the physics engine.

Physics engines for games use the Rigid Body dynamics system of creating realistic motion. This simply means that instead of objects being static in the 3D world, they can have the following properties:

  • Mass
  • Gravity
  • Velocity
  • Friction

While more crucial in game engines than in 3D animation, collision detection is the way we analyze our 3D world for inter-object collisions. By giving an object a Collider component, we are effectively placing an invisible net around it. This net mimics its shape and is in charge of reporting any collisions with other colliders, making the game engine respond accordingly. For example, in a ten-pin bowling game, a simple spherical collider will surround the ball, while the pins themselves will have either a simple capsule collider, or for a more realistic collision, employ a Mesh collider. On impact, the colliders of any affected objects will report to the physics engine, which will dictate their reaction, based on the direction of impact, speed, and other factors.

Practical

Basics

  • Open Assets/Scenes/BaseScene.
  • Make a cube, add a Rigidbody component, press play. Watch it fall forever. This is the gravity simulation.
  • Add a Floor Prototype prefab. Make the coordinates (Position) x: 0 y: 0 z: 0.
  • Press play.

Also, if you change the Physic Material on the Box Collider, this will change how the box interacts with other surfaces.

  • Select a cube, select the bouncy material.
  • Press play. They should bounce.

Using Colliders and Rigidbodies in the Editor

  • Open 1_Physics - Basics scene
  • Import funny shaped models
  • Add Colliders, Rigidbodies, check settings
  • Making composite shapes from Colliders for efficient collision checking
  • Add Physic Materials to make stuff bounce
  • Play around with mass and drag settings to understand the effects.

Using OnTrigger or OnCollision

  • Open 2_Physics - Triggers scene
  • Make a transparent box, open the template script PlaySoundOnTrigger
  • You will have to fill in how it works. What it should do is play a sound whenever the player enters the collider volume.
  • For bonus points, how can you ensure that the collider will be a trigger?

Add forces

  • Open the hover pad script and make it add a force if the player is staying in the trigger volume.

Destroy OnCollision

  • If something gets hit, make it destroy it self and make an explosition using the particle systems in the standard assets.

Make a banging door with Joints.

  • For the brave, make a door that makes a noise when it shuts.

Interactions

  • MouseDown, clicking things in the screen space
  • Raycasting, shooting lasers to hit things in space
  • HitEvent, moving the process of object effects on the objects themselves

Mouse Events

Unity gives you a pile of events on MonoBehaviour that work with mouse input

    void OnMouseUpAsButton()
    {
        // Filters on and off mouse to give button effect
    }

    private void OnMouseUp()
    {
        // Check main mouse button release
    }

    private void OnMouseOver()
    {
        // Maybe change colour
    }

    private void OnMouseExit()
    {
        // Leaving the boundary of an object collider
    }

    private void OnMouseEnter()
    {
      // Entering collider of that object with mouses cursor
    }

    private void OnMouseDrag()
    {
      // Simple way to drag physical object with a cursor.
    }

    private void OnMouseDown()
    {
      // Get first down event from mouse button one
    }
  • Open 3_Interaction - Mouse scene
  • Fill in the blanks on the MouseDown.cs script to make the head fly off when you click on them.
  • Add theDragRigidbody.csscript to each head and see if you can get them to slide around.
  • Experiment with different force types in the AddForce call.

Raycasting

raycast

  • Open 4_Interaction - Raycasting scene
  • Fill in the blanks on the Raycasting.cs script to make the head fly off when you click on them.
  • Open the HitEvent.csscript and transfer the same idea from head to the boxes.
  • Experiment with
    • different force types in the AddForce call.
    • rigidbody mass
    • rigidbody drag
  • Add a Cross hair UI to the scene so you can see the centre point.

Bonus

  • Ragdoll Wizard

    • Open Ragdoll scene, press play, watch for a change.
    • Try to bring it back to life by adding forces to the joints. ragdoll
  • Klak - creative coding kit for Unity that you can prototype things with then steal the code

Show all projects by VizRCA