Tuesday, February 28, 2017

Day 15

Today I continued my tutorial.

Most of today is just scripting so there not many pictures to show.

I added an explosion system into my game so that whenever you shoot an asteroid it explodes, and when the player ship touches one they both explode.

I started off by making 2 public GameObject's and naming them explosion and playerExplosion.

And then I used the OnTriggerEnter method to make something went you enter the collider. So i set the name of the collider to 'other' and then proceeded to write my code.

Inside of OnTriggerEnter I made the asteroid be destroyed when in contact with a game object and then I made it instantiate an explosion at the transform.position and with the transform.rotation.

and then I did the same with the player, I made it destroy in contact with a game object and then I made it instantiate an explosion at other.transform.position with the other.transform.rotation.

What this did is cause a problem, whenever I would spawn in the game the explosions would happen as soon as it starts and the asteroid and players would disappear. What I had to figure out is what the asteroid and player were colliding with to cause this

So I used the Debug.Log() command which writes whatever you give it inside Unity's personal console, So i set Debug.Log() to display the name of 'other'.

What it turned out to be was that sneaky boundary we set a few days ago. Since the boundary is covering the play area it makes contact as soon as the game spawn causing the boundary to be destroyed taking the player and asteroid with it too.

So the way to approach this problem was by making a the objects have tags and choosing which ones to ignore and which ones to collide with.

I made a new tag for boundary and set it to 'boundary'. And I also made one for the player and set it to 'player'.
Now what I did was made an "if" statement and said if other.tag == boundary { return; }.
What return does is it makes it return back to the top of the method until it activates something else.

And I used the same if statement with player instead of the boundary and instantiated the explosion in that if statement.

This is the entire code:


This is what game looks like now:

Tomorrow I will be continuing my tutorial.

No comments:

Post a Comment