Getting Started with Physics in Unity

Written by: Melanie Leemans

I was keen to learn abit more about the physics components this week in Unity, and decided to create a Rube Goldberg Machine. From thinking it would be a quick project, it took me longer than i thought!

My weakest subject at school was Physics and the cause of many nightmares as exams came close. Plenty Plenty Plenty…….. years later, physics is still not my strong point, and so I decided to build a Rube Goldberg Machine.

For anyone that doesn’t know what this is, here is the Wikipedia explanation:

A Rube Goldberg machine is a machine intentionally designed to perform a simple task in an indirect and overcomplicated fashion. Often, these machines consist of a series of simple devices that are linked together to produce a domino effect, in which each device triggers the next one, and the original goal is achieved only after many steps.

Here is my experience with creating it, and wasn’t as simple as I thought even though it’s literally just physics components that get applied to Game Objects, no coding at all!

Setting up Unity Project and Idea

  • Created a new 3D project
  • Created a new scene
  • Created my various folders (my ocd to have everything in its place)
  • I then drew out on good old tree bark(paper) what the pattern should be.
    • What reactions do i want,
    • How do I want the ball to flow,
    • How should the ball start and enter the scene and,
    • What should the end result be.

Preplanning Sketch

  • I also used some 21st century resources and googled a few ideas of how I want my Rube Goldberg Machine to run
  • So in theory this was my idea of how I wanted my machine to work:
    • Ball drops in and lands on declined platform
    • Ball rolls down and lands on next hinged flat level
      • Now here the ball can either roll to the right and hit a Hinged wall
        • Pushing up a block at the top of the wall
        • Pushing a second ball off the level
        • Dropping down a few levels
        • Hitting previous hinged flat level
        • Rolls down, and drops onto last level
        • Rolls into dominos causing dominos to fall over
        • And then drops onto the ground
      • OR
        • Disabled hinged wall
        • Balls rolls down level
        • Drops to lower level
        • Rolls down, and drops onto last level
        • Rolls into dominos causing dominos to fall over
        • And then drops onto the ground

Build out the scene

  • With having my drawing I could now plan out what assets and Game Objects I needed.
  • Getting the different levels above each other, setting up the walls, the declined levels etc, took me majority of the time I used to build out this test project.
  • The whole scene is literally, planes, cubes, and spheres.
  • I kept it super simple as I wanted to focus on the physics part
  • I create a new material for each object by:
    • Right clicking anywhere in the project area
    • Click on Create
    • Click on Material
    • For this exercise I literally just change the Albedo colour under Main Maps

Creating a new Material

  • Dragged the newly created material onto my Game object under Mesh Renderer component

Changing default material

  • This is how the rough built out idea looked

Rube Goldberg Machine Version 1

Adding Hinge Joint Physics

  • The first thing that needed physics was the balls.
  • I added Rigidbody to each ball and left the settings as default

Rigid Body

  • I placed the ball in the air, and hit the play button so that I could test that the physics on the balls were working.
  • I then chose which gameobject I wanted to have the hinged joint component on, which was the longest level in this scene
  • I added the component by

    • Selected the object
    • clicked on add component
    • Typed in hinge joint
    • Selected it.

      Hinge Joint Component

  • Again I tested to see what the effect would be……. nothing……. nothing was happening.

  • After playing with the hinge joint settings for awhile I realised that the axis value on which the hinge should move was wrong.
  • Setting the Axis setting on Z to 1 and on X and Y to 0 created the see-saw effect I was after.
  • I left all the other settings as default.

    Hinge Joint Settings

  • Thus far, I have a ball dropping onto a level, rolls down and then drops onto my hinged level.

  • The problem that was coming in was that my see-saw was dropping to the left, when i needed it to drop the right to avoid the ball running off the edge, and missing the targeted dominos totally.

I have a feeling this has to do with my game objects (level) weight not being dispersed equally like a well balanced scale, but did not want to spend too much time troubleshooting this.

  • So as a quick fix i added a cube to the right of the see-saw level to balance out the weight, and forcing my ball to roll to the right.
  • While the ball is rolling to the right, it’s becoming heavier on the right, and therefore causing the see-saw to open up a space between the cube, the wall and next level which the ball can drop through.

    3D cube

  • Finally the ball rolls onto the bottom most level and hits the dominos before falling to the ground.

    Result version 1

Adding Hinge component to “Hammer Wall”

  • The other option I was playing around with was to remove the hinge joint from the longest level and add it to the yellow wall.
  • You will see in the diagram below that this wall has a “hammer head” at the top, with another ball (rigid body component) positioned in front of it.
  • Ball 1 drops from the sky,
  • It rolls down one level,
  • Runs down next level,
  • Bumps against yellow wall,
  • Pushes wall away
  • Pushes hammer forward
  • Pushing ball 2 off the top level

Hinge Joint "Hammer Wall"

  • Ball rolls down all the levels
  • Gets to the ground
  • Ball 1 is still rolling while ball 2 is doing its thing, and will hit the dominos flat as per example 1

Adding Spring joint to GameObject

  • While tinkering away, I decided to change the way my ball was dropping in and wanted to test out the spring joint.
  • I created a box to house the ball ,with cubes

Ball rolling

Sprint Joint Component

  • I added the spring joint component to the box game object
  • Added the rigid body component to this game object
  • I left all settings as default except freezing the rotation:

    • Under rigid body component settings
    • Constraints
    • Tick all 3 boxes for axis X Y Z next to rotation

      Freeze Rotation

  • Now it bounces like a spring

  • I Rotated the whole box a bit forward so the ball bounces out on start and onto the platform
  • The higher you place your ball, the more force it would apply to the box and therefore the spring effect will be larger and project or bounce higher.

    Spring Effect

Issues encountered

  • Physics component not working

    • Check on which axis you are marking as 1
    • Check that an object that has the physics component attached isn’t in direct contact with another solid (something with a mesh boundary). This will cause your object that has the physics component attached to it to act very strangely, like mine shook uncontrollably
  • From the moment the scene started, physics started simulating, instead of only simulating on being touched

    • untick gravity on the rigidbody component. This will cause the object only to react when it is interacted with.
  • Game object with Spring joint was bouncing everywhere

    • Freeze rotation for X Y and Z.
    • The bounce effect will be more like a spring and not a bungee cord.
  • Ball is rolling off the edges of the levels
    • I didn’t have edges to my levels. I added a few edges to keep the ball on track if it happens to veer to the side of the platforms

Finishing Off

While writing this blog, I carried on tweaking things here and there, as I seem to be enjoying this challenge much more than I expected. I added a few more boxes, springs and ledges,smoothed out some effects, and added more domino’s.

Here is my final result which I am not going to stop refining as I have big plans for what started off being a small project.

I will post my updates here as I go.

Spring Effect