How to Make Video Games 26

Legend of Zelda CloneIn this part of my How to Make Video Games Tutorial we’ll cover how to animated Link, moving Link, creating a game board array and numerous other things. Here is a link to the Sprite Sheet I’m using.

I decided to show 2 ways to create Link and the game board to show the positives and negatives of both. Below you’ll find all the code used in this tutorial. Feel free to do what ever you’d like with it.

If you like videos like this consider donating $1, or simply turn off Ad Block. Both help a lot.

[googleplusone]

Code From the Video

Link.cs

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

// Used so I can use Floor
using System;

public class Link : MonoBehaviour {
	
	// Speed Link moves
	public float speed;

	// Used to move Link
	private Rigidbody2D rb;

	// Used to access the function IsValidSpace in Gameboard.cs
	Gameboard gameBoard;

	// Get reference to the SoundManager for functions
	// SoundManager soundManager;

	// Used to change Link animations
	Animator animator;

	// Makes sure components have been created when the 
	// game starts
	void Awake(){
		// Get Links Rigidbody
		rb = GetComponent<Rigidbody2D> ();

		// Setup Gameboard so I can access functions
		gameBoard = FindObjectOfType(typeof(Gameboard)) as Gameboard;

		// Get SoundManager reference
		// soundManager = GameObject.Find ("SoundManager").GetComponent<SoundManager> ();

		// Get Animator reference
		animator = GetComponent<Animator>();
	}

	// Used to flip the animations
	bool facingRight = false;
	bool facingLeft = false;

	void FixedUpdate(){

		// Get keyboard input
		float horzMove = Input.GetAxisRaw ("Horizontal");
		float vertMove = Input.GetAxisRaw ("Vertical");

		if (gameBoard.IsValidSpace ((float)transform.position.x + horzMove,
			(float)transform.position.y + vertMove, horzMove, vertMove)) {

			rb.velocity = new Vector2 (horzMove * speed, vertMove * speed);

		} else {

			rb.velocity = new Vector2 (0,0);

		}

		if (Input.GetKey ("s")) {

			animator.Play ("WalkDown");

		}

		if (Input.GetKey ("w")) {

			animator.Play ("WalkUp");

		}

		if (Input.GetKey ("d")) {

			animator.Play ("WalkRight");

			// Flips the animation to the right
			if (facingLeft) {
				facingRight = true;
				facingLeft = false;
				animator.transform.Rotate (0, 180, 0);
			} else if (!facingLeft) {
				facingRight = true;
			}

		}

		if (Input.GetKey ("a")) {

			animator.Play ("WalkRight");

			// Flips the animation to the left
			if (facingRight) {
				facingLeft = true;
				facingRight = false;
				animator.transform.Rotate (0, 180, 0);
			} else if (!facingRight) {
				facingLeft = true;
			}

		}

		// Switches back to the Idle position if a key is released
		if (Input.GetKeyUp ("s")) {

			animator.Play ("Idle");

			// When stopped Link will move back into a valid position
			float centerX = (float)Math.Round(Convert.ToDouble(transform.position.x));
			float centerY = (float)Math.Round(Convert.ToDouble(transform.position.y));

			transform.position = new Vector2(centerX, centerY);

		}

	}

	void Start () 
	{
		// set the desired aspect ratio (the values in this example are
		// hard-coded for 16:9, but you could make them into public
		// variables instead so you can set them at design time)
		float targetaspect = 16.0f / 9.0f;

		// determine the game window's current aspect ratio
		float windowaspect = (float)Screen.width / (float)Screen.height;

		// current viewport height should be scaled by this amount
		float scaleheight = windowaspect / targetaspect;

		// obtain camera component so we can modify its viewport
		Camera camera = GetComponent<Camera>();

		// if scaled height is less than current height, add letterbox
		if (scaleheight < 1.0f)
		{  
			Rect rect = camera.rect;

			rect.width = 1.0f;
			rect.height = scaleheight;
			rect.x = 0;
			rect.y = (1.0f - scaleheight) / 2.0f;

			camera.rect = rect;
		}
		else // add pillarbox
		{
			float scalewidth = 1.0f / scaleheight;

			Rect rect = camera.rect;

			rect.width = scalewidth;
			rect.height = 1.0f;
			rect.x = (1.0f - scalewidth) / 2.0f;
			rect.y = 0;

			camera.rect = rect;
		}
	}

}

Gameboard.cs

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

// Used so I can use Floor
using System;

public class Gameboard : MonoBehaviour {

    // Holds all game objects

    // TODO Remember to change space under rock to stairs
    // after rock is moved
    // TODO Change to Gem and then Stump after the bush is moved
    // Do the same for Boomarang, Bomb, Bow, Vace

	// Types of objects Hill, Tree, Bush, House, Boomarang,
	// Bomb, Stone, Bow, Vace, Gem, Stairs, Door, Chest
    public string[,] gameObjects = new string[26, 19];

	// Use this for initialization
	void Start () {

        // Add all game objects to array
        AddYRowXRange(0, 0, 25, "Hill");
        AddYRowXRange(18, 0, 25, "Hill");
        AddYRowXRange(3, 0, 7, "Hill");
        AddYRowXRange(4, 0, 7, "Hill");
        AddYRowXRange(5, 0, 6, "Hill");
        AddYRowXRange(14, 0, 4, "Hill");
        AddYRowXRange(15, 0, 4, "Hill");
        AddYRowXRange(9, 7, 9, "Hill");
        AddYRowXRange(10, 7, 9, "Hill");
        AddYRowXRange(14, 7, 9, "Hill");
        AddYRowXRange(15, 7, 11, "Hill");
        AddYRowXRange(13, 15, 22, "Hill");
        AddYRowXRange(12, 15, 22, "Hill");
        AddYRowXRange(5, 15, 22, "Hill");
        AddYRowXRange(4, 15, 22, "Hill");
        AddYRowXRange(3, 15, 22, "Hill");

        AddXColYRange(0, 0, 18, "Hill");
        AddXColYRange(25, 0, 18, "Hill");
        AddXColYRange(10, 1, 14, "Hill");
        AddXColYRange(11, 1, 14, "Hill");
        AddXColYRange(12, 1, 14, "Hill");

        AddYRowXRange(17, 13, 16, "Tree");
        AddYRowXRange(16, 13, 16, "Tree");
        AddYRowXRange(15, 20, 21, "Tree");
        AddYRowXRange(14, 20, 21, "Tree");

        AddYRowXRange(2, 3, 5, "Bush");
        AddYRowXRange(17, 19, 21, "Bush");
        AddXColYRange(1, 7, 10, "Bush");

        AddXColYRange(16, 7, 9, "House");
        AddXColYRange(17, 8, 9, "House");
        AddXColYRange(18, 7, 9, "House");

        // Place single game objects
        gameObjects[8, 11] = "Boomarang";
        gameObjects[1, 17] = "Bomb";
        gameObjects[21, 8] = "Stone";
        gameObjects[23, 4] = "Bow";
        gameObjects[17, 7] = "Door";

        // Add Link to Scene
        // Drag 53 & 54 to Scene call Animation Idle
        // Rename GameObject Link and drag into Link
        // Empty. Set Empty to X0 Y0 and Link to X1 Y1
        // Change Link Sorting Layer to Link

    }
	
	// Update is called once per frame
	void Update () {
		
	}

    // Adds game object name to array using rows
    void AddYRowXRange(int yRow, int xStart, int xEnd, string gOName)
    {
        for (int i = xStart; i <= xEnd; i++)
        {
            gameObjects[i, yRow] = gOName;
        }
    }

    // Adds game object name to array using columns
    void AddXColYRange(int xColumn, int yStart, int yEnd, string gOName)
    {
        for (int i = yStart; i <= yEnd; i++)
        {
            gameObjects[xColumn, i] = gOName;
        }
    }

	public bool IsValidSpace(float x, float y, float horzMove, float vertMove){

		x = horzMove < 0 ? x + 1 : x;
		y = vertMove < 0 ? y + 1 : y;

		x = (float)Math.Floor(Convert.ToDouble(x));
		y = (float)Math.Floor(Convert.ToDouble(y));

		if (gameObjects [(int)x, (int)y] == null) {
			return true;
		} else {
			return false;
		}

	}
}

Leave a Reply

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