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 - write the collision detection code

Pop open the CharacterScript and let's get bizzay.

  1. Type up the following function within the script. Be sure to follow the rules we've learned about where to properly write a new function (that is, "outside" of your other functions):
    function OnCollisionEnter(col : Collision)
    {
    if(col.gameObject.tag == "bomb")
    {
    // I got hit by a bomb!
    }
    else if (col.gameObject.tag == "stein")
    {
    animation.Play("catch"); // Ima catch that stein!
    }
    col.gameObject.transform.position.y = 50;
    col.gameObject.transform.position.x = Random.Range(0,60);
    }
    

Once again, our prior knowledge of useful game code serves us well. This line should already look familiar:

function OnCollisionEnter(col : Collision) {

We're declaring ...

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