Interactivity : Workshop 6

/
0 Comments
To begin with we created a script that would get rid of the asteroid after being hit with a bolt. However this first script created would completely destroy the asteroid as soon as play mode was activated, only appearing in edit mode. To fix this bug we re-vised the coding, adding a debug. 

Original C# script

Game in play mode - asteroid automatically destroyed


Revised script with debug

However this did not completely fix the problem. When in play mode, boundary was missing from the Hierarchy. 

Missing boundary

To prevent this from being destroyed we added a tag. While selecting boundary in the hierarchy, in the inspector I used the drop down menu in the Tag section to select boundary, therefore tagging it as boundary rather than leaving it untagged


We then went back into the script to destroy the asteroids, removed the debug and replaced it with an IF statement, creating an endless loop where destroy would not be executed before being hit.



The result of this was that the asteroid was now visible in play mode, rotating. When a bolt was fired and hit the asteroid, both bolt and asteroid where destroyed simultaneously.

We then went on to create an explosion effect, when the asteroids were hit to add more realism, as objects do not just simply disappear when attacked. We went back into the previous destroy script and created a public GameObject to create the explosion. Then an Instantiate was also added to make sure the explosion happened in the exact same position as where the asteroid was.


The public GameObject code


Instantiate code


Complete script for the destruction of the asteroid

Next to add the actual explosion effect, I went to the folder VFX and then chose explosions. Then I dragged the selected explosion onto the destroy script in the inspector. This was so it would create a reference in the script.  

Folders for VFX

Explosion effects to drag to script

Effect successfully referenced in script

The result was that when the asteroid was hit with a bolt, a nice explosion effect would occur. 



Next we created a separate explosion for the spaceship crashing to distinguish this from the asteroid and make the game more visually interesting. To do this we went back into the script to destroy and added the following :

public GameObject


Instantiate for player explosion


Completed script with player explosion included

With Player selected in the Hierarchy, I changed the tag in the Inspector from Untagged to Player.

Tag changed to player

Similarly to before, back in the VFX folder, I dragged another explosion effect for the player into player explosion tab in the destroy script in the Inspector to reference the effect in the script and display it in the game. 




The result was a much larger explosion when the ship came in contact with the asteroid. 


The final step with the asteroid was to make it move towards the player and fall from the top half of the screen.  To begin with the Asteroid selected in the Hierarchy, I moved the Mover script from the Assets folder onto the Asteroid game object in the Inspector. 


Then in the speed tab I changed the value to -5. The negative value allows the asteroid to fall down the screen towards the player, rather than up.


Next I selected the asteroid game object in the Hierarchy and dragged it to the prefabs folder in assets. 


Then the asteroid object in the Hierarchy was deleted. 


To begin  allowing spawning of multiple asteroids at once, we then added an empty object called GameController and tagged this, in the same fashion as earlier, as gamecontroller. We also added more coding to the GameController script. 

Tagged gamecontroller

Completed script for spawning random positions

This resulted in a single asteroid being dropped from a random point each time play mode was activated. 



Two examples of random spawn points

After coding some more, in the Hierarchy with Game Controller selected, the hazard count in the script in the Inspector was changed from to 10 to spawn multiple asteroids at a time.


Next we created coding so that we could have a delay between spawns. Then in the Game Controller script in the Inspector I changed the Spawn Wait tab to 0.5 and the Start Wait to 1. 



The result :


Completed code for spawning

Next we created a new script called DestroyByTime to be added on to the VFX explosion prefabs. 


To add this I selected the prefab explosion and then in the Inspector chose Add Component > Scripts > DestroyByTime.


Finally we changed the Lifetime value to 2 in the Script in the Inspector. 



You may also like

No comments: