How to Make Video Games 4

How to Make Space InvadersThis time we’ll make Space Invaders and learn a ton about making complex games. I’ll cover How to Draw Pixel Art with Gimp, Sprite Sheets, SoundManager, Camera, Animating Bullets, User Interfaces, Prfabs, Collisions, Ignoring Collisions, Sound Effects, Making Objects Appear, OnTriggerEnter, SpriteRenderer, Changing Sprites, Destroying Objects, AI Players, Randomizing AI Actions, StartCoroutine, Yield, Animated Explosions and numerous tips that will help you avoid mistakes. The Images and Sounds used are here. This will be a 4 part tutorial. I hope you enjoy it 🙂

If you like videos like this consider donating $1 on Patreon

[googleplusone]

Code From the Video

SpaceShip.cs

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

public class SpaceShip : MonoBehaviour {

	public float speed = 30;

	// Holds a reference to Bullet objects
	public GameObject theBullet;

	void FixedUpdate(){
		float horzMove = Input.GetAxisRaw ("Horizontal");

		GetComponent<Rigidbody2D> ().velocity = new Vector2 (horzMove, 0) * speed;

	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		// GetButtonDown will only fire once per click 
		// Jump is assigned to the Space Bar
		if (Input.GetButtonDown ("Jump")) {

			// Create a Bullet at transform.position which
			// is the ships current location
			// Quaternion.identity adds Bullet with no rotation
			Instantiate (theBullet, transform.position, Quaternion.identity);

			// Play bullet fire sound
			SoundManager.Instance.PlayOneShot (SoundManager.Instance.bulletFire);
		}

	}
}

SoundManager.cs

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

public class SoundManager : MonoBehaviour {

	// Holds the single instance of the SoundManager that 
	// you can access from any script
	public static SoundManager Instance = null;

	// All sound effects in the game
	// All are public so you can set them in the Inspector
	public AudioClip alienBuzz1;
	public AudioClip alienBuzz2;
	public AudioClip alienDies;
	public AudioClip bulletFire;
	public AudioClip shipExplosion;

	// Refers to the audio source added to the SoundManager
	// to play sound effects
	private AudioSource soundEffectAudio;

	// Use this for initialization
	void Start() {

		// This is a singleton that makes sure you only
		// ever have one Sound Manager
		// If there is any other Sound Manager created destroy it
		if (Instance == null) {
			Instance = this;
		} else if (Instance != this) {
			Destroy (gameObject);
		}

		AudioSource theSource = GetComponent<AudioSource> ();
		soundEffectAudio = theSource;

	}

	// Other GameObjects can call this to play sounds
	public void PlayOneShot(AudioClip clip) {
		soundEffectAudio.PlayOneShot(clip);
	}
}

Leave a Reply

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