Time for action - create the HeartBounce Script

We need to attach a new script to the heart. The script will respond to two important situations: when the heart hits the tray, and when the heart misses the tray completely and dive-bombs the Great Beyond. Let's create the script and add some simple code to it.

  1. Right-click/secondary-click the Project panel (or use the Create button) and choose Create | JavaScript.
  2. Rename the new script HeartBounce.
  3. Double-click to open the script in the script editor.
  4. Add the following code to the top of the script above the Update function:
    function OnCollisionEnter(col : Collision) {
    if(col.gameObject.tag == "tray") {
    Debug.Log("yes! hit tray!");
    }
    }
    

What just happened charting a collision course

There's some new ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.