UNITY LINK

Wrecking Ball

Basic principles of working with UnityLink.

In this first project, we create a simple simulation game that shows many of the principles of working with scenes, game objects, components, prefabs and physics.

Project Setup

We start by loading the UnityLink package:

Next, create a new project called WreckingBall in your home directory:

  • By default, the project path passed into UnityOpen is relative to the $HomeDirectory. However, UnityOpen also accepts absolute paths.

Create five asset directories that we will use to keep the project organized:

  • While it is possible to create directories and files in the Assets directory using the standard file system functions, it is preferrable to use the Unity asset functions. They allow the Unity asset database to properly track any changes to files/directories and respond accordingly.
  • Create a new scene and save it to the Scenes directory:

  • The Unity creation functions accepts both Strings and File objects. When a String is provided, the created object (mesh, material, game object, etc.) is only stored in the working copy of the scene. To store the object in the Assets directory, we provide a File object with a path relative to the Assets directory. The Assets directory path is given by $UnityAssetsDirectory.
  • Create three texture assets from images and save them to the Textures directory:

  • We save the textures to the Assets directory to allow them to be used in the standalone application after we build the game. Assets must also be stored in the Assets directory in order to be shared between multiple scenes, though this is not necessary here.
  • Create three materials, one for each texture, and save them to the Materials directory:

  • Textures can not be applied directly to an object in the scene. Instead, we use materials that contain references to the textures. Materials can also store and reference other data such as colors, shaders, and normal maps.
  • Scene Setup

    Create a square platform centered at the origin that is 20x20 units large.

    • UnityLink contains functions for creating all the Unity mesh primitives (plane, cube, sphere, cylinder, capsule, and quad).

    Assign the Caution material to the platform game object:

  • An game objects material can be set using either the Material or SharedMaterial property. Material should only be used at runtime, as using it when the editor is not playing will cause memory leaks.
  • Create a Unity cube called Box and assign its material to be the Box material:

  • Properties can be assigned directory in the Unity creation functions using the Properties option.
  • Add a rigidbody component to the box:

  • Rigidbody components allow objects to move when forces are applied to them, such as gravity and collision forces.
  • Create a prefab from the box and save it to the Prefabs directory:

  • When a game object is stored in the Assets directory, it is known as a prefab. A prefab can be instantiated numerous times within a scene and across multiple scenes. Each instance will reference the original prefab, making it easy to propagate any changes in the future.
  • Delete the original box object since we no longer need it:

    Get some positions for the boxes that will make up our wall:

    Create an empty game object to be the parent of all the wall boxes:

    Instantiate a box at each position and make it a child of the wall object:

    Create a Unity sphere primitive to act as our wrecking ball:

    Attach a hinge component to the wrecking ball and set its anchor to be above the wall:

  • The HingeJoint component constrains the attached object to act as if it is attached to a hinge at some anchor point.
  • Increase the mass of the ball to give it more power:

    Create a pole from the wrecking ball to its anchor point:

    Assign the material to the ball:

    Start the simulation:

    Stop the simulation:

    Additional Changes

    Move the camera to get a better view of the walls:

    Use UnityCameraImage to get the view of the main camera:

    Find the wall parent object:

    Duplicate the wall to add more layers:

    Delete our original wall object:

    Start the simulation and immediately pause it:

    Get the initial frame:

    Step 100 frames to when the wrecking ball first hits the boxes:

    Finish the simulation:

    Get the last frame:

    Stop the simulation:

    Save the scene: