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 - tweak the bounce

Now that we can detect when the heart is hitting the tray, there's no end to the fun we can have! While we're in the code, let's make a quick change to make the gameplay slightly better.

  1. Double-click to open the HeartBounce script.
  2. Type the following at the top of the script:
    var velocityWasStored = false;
    var storedVelocity : Vector3;
    function OnCollisionEnter(col : Collision) {
    if(col.gameObject.tag == "tray") {
    Debug.Log("yes! hit tray!");
    if (!velocityWasStored) {
    storedVelocity = rigidbody.velocity;
    velocityWasStored = true;
    }
    rigidbody.velocity.y = storedVelocity.y;
    }
    }
    

Save the script and test the game. You may not notice a difference at first, but we've made a clear improvement to our mechanic.

What just ...

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