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.
Right-click/alternate-click the Project panel (or use the Create button) and choose Create | JavaScript.
Rename the new script
HeartBounce
.Double-click to open the script in the script editor.
Add the following code to the top of the script above the
Update
function:function OnCollisionEnter(col : Collision) { if(col.gameObject.CompareTag("tray")) { Debug.Log("yes! hit tray!"); } }
What just happened – charting a collision course
There's some new code happening here. OnCollisionEnter
is a built-in function that gets called when the GameObject's collider touches (or collides with) another GameObject's collider, as long as one of those colliders is attached to a GameObject with...