Breaking glass Effect PART II

After setting up the scene, the scene initialisation through scripting follows. I always write C# scripts, in order to have a more formal language checking during compilation, and because there are several external libraries and code snippets out there in C#. But this is a personal view that somebody else might not share. Feel free to follow your personal taste in this matter (and write in Javascript or in Boo).



Scene Initialisation

The initialisation is implemented with a Startup script, applied to the camera, since the camera is an indispensable scene object.

The Start method is as follows:
using UnityEngine;
using System.Collections;
 
 // Use this for initialization
 void Start () {
  Debug.Log ("Up and running baby!");
  
  // Procedurally create a cube that will act as the floor
  GameObject myCube = CreateCube("GlassTerrain");
  originalXCameraAngle = this.transform.eulerAngles.x;
  originalZCameraAngle = this.transform.eulerAngles.z;
 } 

Initially I had a static gameobject in the scene hierarchy, same as the camera and the other scene objects. Unfortunately this decision meant that I had to re-run the game prototype each time the glass object was broken (which is the main goal of this project). I then switched to having a prefab object, and created procedurally this prefab. But the solution was even simpler, and it was more funny and challenging for me to script it: create a primitive cube instead of the prefab. This way I would have full control over the properties of the game object. And without further ado, the CreateCube method was scripted that is taking care of the procedural generation of the glass surface:

 GameObject CreateCube(string cubeName) {
  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  cube.name = cubeName;
  cube.tag = "GlassMaterial";
  cube.AddComponent <Rigidbody>();
  cube.AddComponent <calcCollisionImpact>();
  cube.renderer.enabled = true;
  cube.GetComponent<Renderer>().material = cubeMaterial;
  cube.GetComponent<BoxCollider>().material = cubeColliderMaterial;
  cube.GetComponent<calcCollisionImpact>().shardPhysicMaterial = cubeColliderMaterial;
  cube.GetComponent<calcCollisionImpact>().shardMaterial = cubeMaterial;
  cube.GetComponent<calcCollisionImpact>().GlassBreakingSound = glassBreakingSound;
  cube.GetComponent<calcCollisionImpact>().BreakingForceThreshold = BreakingForceThreshold;
  cube.GetComponent<calcCollisionImpact>().CrackingForceThreshold = CrackingForceThreshold;
  cube.GetComponent<calcCollisionImpact>().BrokenGlassMaterial = brokenGlassMaterial;
  cube.GetComponent<Rigidbody>().mass = 0.3f;
  cube.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
  cube.GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
  cube.transform.position = new Vector3(1.91f, -2.00f, -9.92f);
  cube.transform.localScale = new Vector3(1.975f, 0.25f, 1.9625f);
  return cube;
 }

This method is using some of the script public variables, such as the cubeMaterial, the cubeColliderMaterial, etc. These variables are passed to the components of the cube object in order for these components to be properly setup.
Some hard-coded transformation is happening at the end of the method, positioning and scaling, to set the new object at the right position and scale in the scene under the camera.

GameObject.CreatePrimitive(...)  generates a new glass cube gameobject. Some components such as meshfilter and renderer are automatically added to the new game object by default. Those components that  are not, are added explicitly one by one in the script.

Public variables


The script exposes a set of public variables that the user should set through the Inspector of the Unity Editor before executing or building the game prototype.
These are explained in the code below:

public class startup : MonoBehaviour {
    public GameObject Sphere; // points to the prefabricated sphere object of the ball(s) to be thrown at the glass object
    public PhysicMaterial cubeColliderMaterial; // points to the cube's collider material (there are 2 available prefabs in the project from the standard Unity assets)
    public Material cubeMaterial; // points to the material for the cube (1 such simple material exists in the project folders having a transparent/difuse shader)
    public Material brokenGlassMaterial; // similarly, another simple material exists having a texture of a broken glass
    public Material sphereMaterial; // points to the material used for the sphere
    public AudioClip glassBreakingSound; // points to a short audio clip of a glass cracking/shuttering sound in the project folders
    public float CrackingForceThreshold = 11; // this is the force required to get the glass cracked -but not yet broken
    public float BreakingForceThreshold = 13; // this is the force required to break the glass object to pieces


Further to that, a set of private variables is defined to be used internally in the script:
    private float ClickPeriod; // gets the time period between the click and release of the left mouse button
    private const float ForceMultiplierConst = 10; // a constant used to calculate the force applied on the ball when it is released
    private int sphereNumber = 0; // counts the balls released so far, and helps setting a unique name for each one
    private float originalXCameraAngle; // keeps the original rotation of the camera around the X axis, used for interpreting the camera rotation in relation to the keyboard input
    private float originalZCameraAngle; // similarly for the Z axis

In the next Part III, we will cover the Update mechanism, basically how the objects interact with each other, and then in the final part IV I will focus in the main course: how the glass object is broken to shards, how the new shard meshes are generated.

Comments