Time for action – write the collision detection code
Pop open the Character Script and let's get bizzay.
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.CompareTag("bomb")) { // I got hit by a bomb! } else if (col.gameObject.CompareTag("stein")) { animation.CrossFade("catch"); // Ima catch that stein! } col.gameObject.transform.position.y = 50; col.gameObject.transform.position.z = -16; 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 a function—in this case, a special built-in Unity function called OnCollisionEnter
—and accepting a variable called col
as an argument. The value of col
is a collision...