O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required