This 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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