Interactivity - Lesson No.4

Within this lesson we looked at making the bolts shoot from the spaceship, and also adding in hazards (the asteroids) and making the spin a little bit to give them that in space kind of feel.

Making the bolts move

The first thing to do is create a new C# script for the bolt to make it move, it's a pretty simple script that allows the bolt to shoot forward on the z axis (shown as transform.forward in the script). 

Now all the needs to be done is to change the speed of the bolt to 20 in inspector window and then save the bolt as a prefab, this is because it is going to be used a lot in the game and doesn't need to be on screen at all times. To do this all that needs to be done is drag the bolt into the prefab folder and it's there, and then delete the bolt from the Hierarchy so it's not constantly in the game. To test if the prefab works just drag it into the Hierarchy when playing in game view and it should shoot across the screen. 


Boundary

However once these bolts had been created they aren't disappearing from the Hierarchy, this can cause problems with overloading the prefab meaning the game can lag or stop working. To fix this a boundary needs to be created to destroy the bolts once they've left the screen, this will also destroy any other elements in the game if they leave the scene. 

The first thing that needs to be done to do this is create a new cube which is under 3D object, then rename it boundary and reset the position. A cube should then appear in both the scene and the game view, this then needs to be transformed to fit the scene completely creating the boundary, making sure that the top and bottom of the boundary fits the scene perfectly. 


Once this has been done the mesh renderer and the mesh filter on the cube need to be deleted, this is because the cube does not have to be visible on the game view. 
Next is time to create a new script for the boundary and call it DestroyByBoundary, and within the script will be the following:

What this means is that the moment the collider on the bolt hits the edge of the boundary it will disappear, thats all done through the OnTriggerExit method.

Creating Hazards

We then went onto creating the asteroids and putting them into the scene, first of all we needed to create a new empty GameObject to attach the asteroid model too. The GameObject also needs to be renamed to Asteroid, its origin reset, and moved up the scene first before attaching the asteroid model as a child. 
Within the model folder there are three different asteroids, we used the first one on their and dragged it on the empty GameObject which would then become the parent of the asteroid model. 


Next the rigidbody component needs to be added onto the GameObject parent and use gravity needs to be deselected, also a capsule collider needs to be added on and shaped over the asteroid until it fits around the model. 


We then went onto making the asteroid rotate to get that in space kind of feel for the game, this was started by creating a script and renaming it RandomRotator. The following screenshot shows the coding that was used within the script; the angular velocity decides how fast a rigidbody object is rotating, and we set that to  Random.insideUnitSphere and also multiplied it by tumble so that we can set how fast it spins in the inspector. 


Once the script has been written and the tumble value changed it can be seen that the spin starts to slow down after a while, this is due to the rigidbody component. To stop this from happening the angular value needs to be changed to 0, and this should allow the asteroid to continue to rotate.

Unknown

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment