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

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.