Create Space shooter game

Create Space shooter game
December 17, 2018 No Comments Development,Game Development Pushpendra Kumar



Let’s start with something creative and new! In this tutorial, you will learn the basics of Unity 3D game development kit. I am going to create the new project with space shooter.

So you people just follow the steps of this tutorial, I make sure you will become professional after doing this work…!!

  1. Open Unity Kit
  2. Click on Create New project
  3. Give Name as Space Shooter
  4. Choose the preferred location
  5. Keep the default templet 3D
  6. No need to add Assets Package
  7. Click on Create Project

It will import the default assets and bring the development area on your screen.

Now Next step is very important and very easy – Go to the Unity Official Asset Store – “LINK”

  1. Search for the Space Shooter into Search Bar. 
  2. Select the Space Shooter tutorial templet. As mention into the images
  3. When you click on that then a pop windows will visible on your screen with option Open in Unity button.
  4. When you will click on this button then it will ask you for the permissions Do you want to allow this page to open “Unity”?“. Say YES..!!
  5. Now hopefully assets will visible into Unity within few seconds with the selected Assets. 
  6. Now Click on Download. 
  7. It will take few seconds or minutes, because it’s around 18 MB19 MB in size. 
  8. Finally after downloading now you will see button would be change from download to import. So it’s time to import the Assets into your project. 
  9. When you click on import a warning message may occur, if yes then do not need to stay just click on import
  10. Now a popup window will come with lot’s of files. Don’t loose any single one. Click on All at the left bottom for selecting all and then click on import.
  11. Now assets successfully imported into your project. You can see the assets into your Project Area. 
  12. Don’t forget to save the scene with Command+S in Mac and Control+S in windows.  
  13. Here I have make some changes in my project. I have rename my Scene folder with _Scene, which is exist into assets folder into project. And rename Sample View With the Main.

Great JOB till now. Now this time to move forward for some advance modification and development. So let’s have look and follow the steps. 

  1. Navigate to your Assets Folder. In Assets folder you will find the Models folder. Click on the Models folder and expand it. Great..!!
  2. Now select vehicle_playerShip and drag it to hierarchy of your project.  Now Click on F
  3. Rename vehicle_playerShip with Player
  4. Reset the Transform of your Player, You can reset your Player Transform from the Inspector window of your Player. In the Transform row you will find the gear icon, click on that and select reset option..!!
  5. Add Component to your player form Inspector Window. Click on Add Component select Physics then select Rigidbody.
  6. Now Deselect Use Gravity option form Rigidbody
  7. Add one more component to your player with the same process, This time you have to add Physics -> Mash Collider
  8. Now change your scene after click on Y is shown into Image..
  9. Update: Please check 'Convex' on the Mesh Collider component, otherwise the Mesh Collider will not participate in physics collisions and will not be visible. Please Note: The convex mesh will look different from the mesh collider shown in the
  10. In the Next Step Open Your Model folder and Add Player_ship_collider to your Mesh Collider of your Player as shown into below image..!!
    An also check Convex and Is Trigger option..!!
  11. Now you need to add engines_playerengines_player is exist into Prefabs -> VFX folder, Select engines_player and drag from project to Hierarchy and drop inside player..!!

Great Job, Now let’s have look on another important step. Which is Camera and lighting

  1. Click on your camera from the hierarchy and reset the transform after click on gear icon..!!
  2. Now set new value to Transform, Position Y=10, Z=5 & Rotation X=90, Keep all value as it as..!!
  3. Set Camera Projection as Orthographic and Size as 10
  4. Set Camra Flag as Solid Color and make background black with Hex Code 000000
  5. Also change the colour of your player lighting – In Unity v5 Window > Rendering > Lighting Setting : Scene Tab.Please remove the Skybox by selecting it and deleting it.Make sure the Skybox is “None”.
  6. Remove the Default Direction Light from the Hierarchy and Add New Direction Light. Rename it as Main Light
  7. Reset Transform of Main Light  & and assign new values as > Rotaion X=20, & Y = -115
  8. Now create a Duplicate light of Main Light and give name as Fill Light. 
  9. Rest the Transform of Fill Light and give Rotation X = 5 & Y = 125. Also Change the Colour of Fill Light with R=128, G=192, B=192. Else you can do from your side.
  10. Again Create a one more duplicate light of Fill Light and give name as Rim Light. Rest the Transform of Rim Light and give Rotation X = -20 & Y = 60. Also Change the Colour of Rim Light with R=255, G=255, B=255(White). 
  11. Now we have three light, Just manage the hierarchy of your project. Create new Empty Game Object and Rename that as Lighting. And don’t forget to reset the Transform of Lighting Object. Drag All three light into Lighting and set Position Y = 100. 

Great Job! Now Let’s have look to Adding a background

  1. Create Quad from Hierarchy, You need to click on Create and there you will see > 3D Object > Quad.
  2. Rename Quad to Background and Reset Transform,  Add New Transform Rotation X=90, Now you can see your Background. 
  3. Now Add Texture to your background, it is very simple to add. Navigate into your asset folder your will find Texture. Click on Texture and see for tile_nebula_green_dff. Now Drag this image and drop into your background are into game scene. And also Remove the Mash Collider from the Background Inspector.
  4. Now Again set your Transform, This time we will work on Scale property. In this slide your X scale to fit with your camera and keep your Y double of your X, so that your background should visible perfect. In My case X=45 & Y=90
  5. Set Background transform position Y=10.
  6. Here you need to Change the Shader of Your background form the Inspector Window, Change your Shader with Unlit > Texture as mention in below image..!!

Well Done..!! Now Let’s work on Moving the player

For Achieving this task we need to write few codes of line in c#. So let’s have look what steps we need to took for this task. 

  1. Create new folder in assets & rename that folder with Scripts
  2. Now Select your Player Object from Hierarchy, Click on Add Component and Select New Script 
  3. Give Script Name as MainPlayerController and hit on enter, Now your Script is exist into Root folder, Drag this file and drop into Scripts folder. 
  4. Now open your file for Editing after double click on the file or you can also open file from the inspector window of file. And write the following code into your Player Controller. 

Great Job..!! Now Check your Unity And Run your project after click Play Button!

Until now I hope everything is working perfect, Here if you have noticed the speed is very very slow. So let’s do some more work on MainPlayerController. 

And Change your player controller with the following code!

Now do one more task..! Open UNITY and select player and navigate to player inspector. Here you will find Main Player Controller (Script). And inside that you must find speed, Here set speed = 10. Great now you can see the changes after playing the game. Now here you’ve must notice that your player is moving out from the game area. So now let’s manage that. Change Your code with the following code.

After adding that set some property to your Unity Inspector as shown in figure below

These property you need to set form inspector of your player.

Now again hit on run button for seeing the changes..!!

Great Job..!! Now we are going to work on another task, which is Creating shots.

So for that follow the bellow procedure.

  1. Create a Empty Game Object in Hierarchy, Rename it with Bolt, Reset Transform.
  2. Create Quad, As we did for the background, Rename it VFX, Reset the Transform and make VFX as Child of bolt object. Now Set Transform Rotation X=90.
  3. Now Create New Material into Assets, Click on Create > Material. Now you see new material into materials folder. Rename this with fx_bolt_orange
  4. Now go to the inspector of fx_bolt_orange and click on Albedo. From that pop search for fx_lazer_orange. Perfect 👌🏻👌🏻
  5. Now Drag your material to VFX Quad.  And From the Inspector Window of VFX, Select the Shader from the bottom. Click on Shader dropdown, Particles > Additive.  Super 👌🏻
  6. Now Click on Bolt and Add a Rigidbody Component from the Add Component > Physics > Rigidbody.  And Uncheck Use Gravity. 
  7. Now Remove the Mesh Collider component of VFX after click on gear menu. 
  8. Add New Component to your Bolt Game Object, Add Component > Physics > Capsule Collider, and set the following property to that. 
Set this properties

Perfect 👌🏻😊 Now Let’s move to another phase. 

Add New Script to your Bolt Game Object. Name as Mover, move it to Script folder and open for editing.  Add the following code to your script. 

And Drag Your Bolt From Hierarchy to Prefabs into Project and Remove Bold From Hierarchy. Now Click on Prefabs and select Bolt now set the speed = 20 as mention in below image.  

Great Job..!!👌🏻 Now let’s Move to Advance option which is Shooting shots. 

  1. Create new empty game object, Rename with Shot Spawn and move it to player game object as a child. Reset the transform and set new position Z=1.25 in my case. Move Z upto your  Shot Spawn will not come in front of your player. 
  2. Now let’s do some work on Player Controller, Now your Player Controller will look Like That after adding Fire Button. 

Perfect 👌🏻, Now Click on Player object  and set your public value as mention into upper image, First Drag Bolt From Prefabs to Shot and Shot Spawn from hierarchy to shot spawn script in inspector. Now Run Your game and Fire after click on our player. 

Great Job..!!  Dude Now let’s move towards another task. Which is Boundaries, Hazards and Enemies

1. Boundary

  • Create Cube in Hierarchy from 3D Object, Reset Transform and Rename with Boundary. 
  • Inside Box Collider of Boundary checked Is Trigger and set new transform. Scale Your Boundary with Background Size in my case – Scale x=12, y=0 & z=30.
  • Add New Script to Boundary and named as DestroyByBoundary, and drag this file into script folder. 
  • Now edit your DestroyByBoundary with the following code.
  • Keep in mind, Delete Mesh Renderer and Mesh Filter Component of Boundary and also make Mesh Collider is selected and also select Is Trigger option inside Mesh Collider. Now Run your game and see the output. Now every shot auto deleted as it leaves boundary.  

That’s what we want at this time, Now let’s have look on another step which is… as

2. Creating hazards

  • Create New empty object and named as Asteroid. Reset Transform and add position z=8. So it would be separate from player. 
  • Now navigate into project > Models and select first asteroid, and drag it into your asteroi d folder. 
  • Now Add Rigidbody and uncheck Use Gravity Option. Add Another Component Capsule Collider. And Set Radious 0.5 and Height = 1.7 and Direction Z axis. 
  • Add New Script to your Astroid and Rename as RandomRotator. Same Drag this into your Script Folder. Write the following code 
  • Save it and return to Unity, and set tumble from unity, You will find tumble in Inspector at the bottom inside script.  tumble = 5 and run your game. 
  • Perfect 👌🏻 Now Add another new component, Means new Script with the name DestroyByContact, and codes as below..!! Also make Is Trigger of Astroid TRUE. 



Don’t forget to set the Tag to your Boundary, Select Boundary, Add new Tag Name as boundary and again select boundary. 👌🏻 Perfect Run your Game and see the output, Now we will work on Explosions. 

3. Explosions

  •  Need to Edit your Code as below..!!
  • Now Return to Unity, Click on PreFabs > VFX > explosion_asteroid, Drag this and Drop into Astroid Destroy By Contact Script same for explosion_player
  • Finally Drag Astroid from hierarchy to Prefabs and Delete it from Hierarchy. 

Great Job..!! Until now you are doing very well and result in front of you..!!😊✨. Now Let’s move towards another step. 

4. Game Controller

  • Create Empty Game Object, Rename as Game Controller and reset transform.
  • Set Pre Made TAG, GameController. 
  • Create New Script for Game Controller object and give name as MyGameController. Drag The MyGameController file into Scritp Folder.  And open MyGameController for Editing.  And Add the Following Code to your Code..!!

And Also Check, Prefabs > Asteroids, Click on Astroids, If Mover Script is exist into Inspector window of Asteroids with value -5, then it’s perfect. Otherwise Click on Astroids, now click on Script folder and drag mover script to inspector window and set the speed with -5. 

Apart from that you need to do little more work. Click on Prefabs folder and drag asteroids to public script variable Hazard, And this Hazard Belongs from Game Controller Object. and set rest of the property as given below..!!

Great JOB..!!, Now Let’s see on another steps which is as Scoring, Finishing and building the game. 

1. Audio

  • Open Audio Folder and Pick Audio and Drag into Prefabs as mention in below picture. 
  • Same do for Player explosion as mention in below picture. 
  • Now Add weapon_player audio from project to Hierarchy as mention into below image..!!
  • Now Click On Player and Navigate to your Audio Source into Inspector. From Audio Source DeSelect Play On Awake. 
  • Now Let’s Do Some Work On Player Controller Script, So Open MainPlayerController and Update with he following code.  Just Update the Update() function. 

Perfect..!! 👌🏻👌🏻 Now  let’s try to run your project and see the output. Great now you have sounds effect in your game..!!

  • Now do one more thing add Select music_background from Audio and Drag into Game Controller. As mention into below images. 
  • And Check Play On Awake option and Loop Option From Audio Source  of Game Controller object inspector window.  as mention into below image. 

Well Done..!!👌🏻 
Now Play Game and see the Out Put, I hope upto now you’ve done upto now very well. So now Let’s move another work which is as. 

2. Counting points and displaying the score

  • Add Text to your Game, Click on Hirarchicy > Create > UI > Text. Reset Transform of Text and Rename it with Score Text and set new position PosX = 10, PosY=-10 & PosZ=0 and set view orientation as mention into belo image. 
  • Perfect now you need to do some editing into your game controller and DestroyByContact script. So first make some changes into GameController Script. Check the changes below. 
  • Now Asing Your Text From UNITY to your Script as mention into below image. 
  • Awesome Now add some code into your DestroyByContact, See the Code Below. 
  • Perfect now return to your unity and click on PrefabsAsteroid. In Astroids set Score Value =10 as mention into below image. 

Perfect Now Run your Game and see the output, I hope now you can see the score on your screen. and also you can see the changes into the game. 

Perfect..!!👌🏻👌🏻 Now let’s work on Ending the game.

3. Ending the game

  • Now Duplicate the Score text two times and set new property for both. As mention in below image. 
  • Restart Game Text 
  • Game Over Text

Perfect now do some modification into your game script. So Open GameController Script and make the following change. 

  • Perfect..!!, Now Let’s Make some changes into Unity. So return to UNITY and do the following changes as shown into the below figure. 
  • Drag and Drop as mention into images.
  • Now need to write some code into DestroyByContact script. Let’s Open and write some code so that we can track the collision…!!💫💫

😊👌🏻 Awesome🤗 Now Run game and see the output. Now game is complete..!! Now Let’s Build the game…..

4. Building the game

  • Open the Build Setting Window..!! File > Build Setting
  • And You will find the following window, Set the Following window intention. 
  • Now, Click on Player Setting and Set Your Company Name..And After Setting that click on Build and Run. Now finally you can Run Your Game. on your stand alone application. 

Wish you Good Luck 😊 Happy Coding



Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *