Interactivity : Workshop 5

/
0 Comments
In this lesson we added movement to our bolt so that the ship would appear to be shooting. 

To allow us to have multiple bolts without creating a new object each time one was fired, we added a Bolt prefab to the prefabs folder in the project window. In the folder I right clicked chose Create > Prefab. This resulted in many bolts visible around the ship in the game view.


Bolt prefab added

In the Hierarchy, I right clicked and selected Create Empty.  I named this empty 'SpawnPoint' to represent where the bolts would be appearing from. 




The SpawnPoint was then placed inside the ship in the centre, just where the bullets would be 'spawning' from. The SpawnPoint was attached to the player in the Hierarchy. This placement can be shown through the blue arrow in the screenshot below :


SpawnPoint attached to player

Next we did more coding which would allow the bolt to be fired from the SpawnPoint when the mouse was clicked. This was also made to reload the bolt and keep firing. I created a C# script called BoltMovement :


In the prefabs folder, I dragged the Bolt on to the project. This is how the game looked in game view :

Bolt is constantly being fired when mouse held down

To prevent the constant release of bolts we made a code to create gaps between firing and then set the fire rate in the inspector 0.2. 



Final bolt movement

Next we created a boundary so that the bolts would be destroyed as soon as they left the edges of the game view. Without this the fired bolts would continue to be shot forever across the scene, even though not visible in the game. 


Boundary for bolt 

We created a new C# script called DestructionByBoundary and attached this so that the bolts would be destroyed before leaving the edges of the boundary. 


Script added to boundary

Next we began adding the asteroids to the game. We created a new game object, named it Asteroid and re-set it to origin, before moving it forwards directly in front of the ship by several paces. 


In the Models folder, I chose the first asteroid model and dragged it on to the Asteroid game object in the hierarchy. The model then appears in the scene view in the game object position. 



Then I re-set the transformation of the asteroid to origin so that it had no relative rotation and is centred on the parent game object. 
Before re-set

After re-set to origin

A rigidbody was added to this in the inspector. Use Gravity was deselected to prevent the asteroid from falling out of the game. A capsule collider was also added as a component to allow it to rotate. 


Capsule collider on asteroid

Finally we created a new script called RandomRotate to attach to the asteroid to make it rotate. 



Successful asteroid rotation feat Shahid! 





You may also like

No comments: