Invading Spaces! – 7/5/12

In todays session, we made a clone of space invaders. With Unity, I always begin with a basic layout I set my level up and this is what I made my level look like.

Once that was done, it was time to start programming basic movements. In programming and making games, the number one thing is organisation. So I made a couple of folders for the things I needed including Scripts, Scenes and Prefabs.

I then started to program the basic movement of my player. This was very similar to pong last week, except rather than making the object move vertical, I made it move horizontally. After this I then put the code in place for the bullet to fire.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerController : MonoBehaviour
{
    private float moveHorizontal;
    private Rigidbody rb;
    public float speed;
    public GameObject bullet;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();   
    }

    // Update is called once per frame
    void Update()
    {
        moveHorizontal = Input.GetAxis("Horizontal");
        rb.velocity = new Vector3(moveHorizontal, 0, 0) * speed * Time.deltaTime;

        if(Input.GetKeyDown(KeyCode.Space))
        {
            Vector3 pos = transform.position + new Vector3(0, 2, 0);
            Instantiate(bullet, pos, Quaternion.identity, null);
        }
    }
}

So whats happening here? Well in the start function, We are telling Unity to grab the rigidbody component from this game object. In update, we are telling unity that if the axis buttons that are matched to horizontal are pressed, then return this value. That value is then used as a velocity which we multiply by speed and Time.deltaTime.

Finally, if we press the space button, then this will instantiate, which means spawn or create, a game object at this objects position + 2 units on the Y axis.

Next, it was time to make the bullets physics happen. The code for this is fairly straight forward and this is what it looks like.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Bullet : MonoBehaviour
{
    private Rigidbody rb;

    public float Speed;

    
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();

    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity += new Vector3(0, 1, 0) * Speed * Time.deltaTime;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Block")
        {
            
            Destroy(this.gameObject);
        }

        if(collision.gameObject.tag == "Bounds")
        {
            Destroy(this.gameObject);
        }
    }
}

Similar to the player object, the bullet object also uses a rigidbody so we grab that in start. In Update() we have it so that the velocity constantly adds 1 unit on the Y axis every frame. Finally we have OnCollisionEnter which is a default Unity function. This basically says that if this object collides with that object then this happens. In this instance, the object is destroyed.

Next, I am going to add a score system and opponents to avoid and destroy in the conquest of becoming the best space invader.


After a couple of more hours working on the game, I have modelled an enemy and managed to add some form of physics to them by making them move left to right, right to left and down when they hit a boundary. I also managed to make a working score system.

Firstly, the score system. As my score system relies on a bullet hitting an enemy object, the score wasn’t counting as the object would be destroyed and so, the score system would not know where to get its information from. I quickly remembered PlayerPrefs.

Player Prefs is a tool built in to Unity that is meant for players of your game to be able to save their settings. However, I have used it here in order to save the score whenever the player received one. In terms of enemy movement, I used the same method as the player movement but without the button press. I then added a collider box to my enemy player and had it so if that collider hits the side boundary, the enemy would move down a row.

The issue with this is that the collider could not be destroyed. So if all the blocks for the enemy were destroyed, and you fired a bullet to the same place, the bullet would get stuck in mid space. I solved this problem by writing code so that if the parent object had no children left, then that object would be destroyed.

To finish it off, I made a game over screen and a main menu. itch.io.

Leave a comment