Thursday, June 11, 2015

[Unity 3D] Tutorial 2: Space Shooter

In this second tutorial they teach you to make a very basic top down space shooter. There is a little more scripting than in the first one and involves using more game objects and features. You learn to:
  • Change the resolution of the game
  • Modify and save a layout
  • Import assets from the Unity Asset Store
  • Work with mesh models, textures and materials
  • Use mesh colliders and capsule colliders
  • Instantiate and destroy prefabs
  • Add background music and sound effects 
  • Use tags to identify game objects
  • Work with coroutines
  • Use the yield instruction
  • End a game and reload it
  • Use methods and parameters like Mathf.repeat,Time.time or angularVelocity
  • Differentiate between screen space and viewport space
The tutorial is again very easy to follow, but it has been developed in Unity 4. If you are using Unity 5 then you will need to make a few modifications to make it work:
  • Mesh colliders won't work unless you enable the Convex flag
  • Some static references such as rigidbody have been removed:
    • Unity 4: 
      • rigidbody.AddForce(movement);
    • Unity 5: 
      • RigidBody rigidbody = GetComponent<Rigidbody>();
      • rigidbody.AddForce(movement);
  • The render settings menu has been moved:
    • Unity 4:
      • Edit > Render settings
    • Unity 5:
      • Windows > Lighting
  • The GUIText is no longer accessible from the Create menu. In Unity 5 you need to create an empty game object and then go to Add component > Rendering > GUIText
And that's all I can remember at the moment. I encountered some of these issues already in the first tutorial but I forgot to mention them in the post.

Finally I added a couple of extras to try new things and make it a bit more interesting: a scrolling background and a blinking text. I will explain how to do it at the end of the post in case someone is interested. I could have added more things like parallax or smarter enemies, but I just want to move on to the next tutorial :-)

And here's the result:


Input settings:
- Move with the arrows
- Shoot with the space bar or the left mouse button


How to make a 2D scrolling background

For the background I followed this live trainning session, which shows several ways to do it. I tried the first two and in the end I kept the version that uses the texture offset to create the effect.You just need to create a Quad, assign it the desired texture and add the following script to it:

public class OffsetBackground : MonoBehaviour 
{

public float scrollSpeed;
private Vector2 originalOffset;

void Start ()
{
originalOffset = GetComponent<Renderer> ().sharedMaterial.GetTextureOffset ("_MainTex");
}

void Update () 
{
float offsetY = Mathf.Repeat (Time.time * scrollSpeed, 1);
Vector2 offset = new Vector2 (0, offsetY);
GetComponent<Renderer> ().sharedMaterial.SetTextureOffset ("_MainTex", offset);
}

void OnDisable ()
{
GetComponent<Renderer> ().sharedMaterial.SetTextureOffset ("_MainTex", originalOffset);
}
}


How to make a blinking text

Making a text blink is very simple. You just need to set the text of the GUI element to blank for a few seconds and then back to its original text for a few more seconds, alternatively. In my case I used it for the "Press 'R' to restart" label.

First I created this coroutine:

IEnumerator BlinkRestartText()
{
while(restart)
{
restartText.text = "Press 'R' to restart";
yield return new WaitForSeconds(blinkingRate);

restartText.text = "";
yield return new WaitForSeconds(blinkingRate);
}
}

And then modified the SpawnWaves method as follows:

if(gameOver) 
{
restart = true;
restartText.text = "Press 'R' to restart";
StartCoroutine(BlinkRestartText());
break;
}

That's all. When the game is over the coroutine is launched and runs until the restart flag is set to false. This happens when the player presses R and the game is reloaded (the method Start will be called by Unity). As for the content of the loop, it will first of all set the text in the GUIText object and wait for a few seconds, and then set it to blank and wait for a few more seconds, and so on. The use of the yield instruction ensures that the game won't be paused while waiting, and that the function will continue from the line after the yield after the wait is over.


No comments:

Post a Comment