How to Make Video Games 29

Legend of Zelda CloneIn this part of my How to Make Video Games series I will continue making a Zelda clone. I’ll create animations for the villains and animate them. I’ll also cover a new way to animate Link’s sword from inside of code. We’ll use coroutines to make items show and then disappear on the screen and a whole lot more.

All of the code used follows the video down below. As always feel free to do what ever you’d like with it.

If you like videos like this consider donating $1, or simply turn off AdBlocker, which helps a lot.

[googleplusone]

Code From the Video

Villain.cs

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

public class Villain : MonoBehaviour {

	// NEW STUFF 1
	public float speed = 2f;

	// The villains will all start going right when loaded
	Vector2 direction = Vector2.right;

	// Keeps track of if Villain is chasing or not
	bool chasing = false;
	// END OF NEW STUFF 1

	// NEW STUFF 2
	// Get reference to the Animator so I can change animations
	private Animator animator;

	// Reference to Link
	GameObject linkGO;

	Rigidbody2D rb;

	void Awake(){
		// Get reference to Link
		linkGO = GameObject.Find("Link");

		// Get reference to rigid body
		rb = GetComponent<Rigidbody2D> ();

		animator = GetComponent<Animator> ();
	}
	// END OF NEW STUFF 2

	// NEW STUFF 1
	void FixedUpdate(){

		// Change the direction as needed
		GetComponent<Rigidbody2D> ().velocity = direction * speed;

		/*
		transform.position = Vector3.MoveTowards (transform.position,
			linkGO.transform.position,
			.03f);
			*/

	}

	// Handles changing direction when we hit VillainStart and end
	void OnTriggerEnter2D(Collider2D col){

		if (!chasing) {
			// When we collide make the Villain go in the opposite direction

			transform.localScale = new Vector2 (-1 * transform.localScale.x,
				transform.localScale.y);

			// Change direction
			direction = new Vector2 (-1 * direction.x, direction.y);
				
		}  
		// END OF NEW STUFF 1

		// NEW STUFF 2
		else {

		}

		// if(col.gameObject.name == "Link")
		// END OF NEW STUFF 2
	}


}

Link2.cs

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

// Used so I can use Floor
using System;

public class Link2 : 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;

	// NEW STUFF 2

	GameObject weaponGO;
	GameObject swordGO;

	public enum AttkDir
	{
		LEFT, RIGHT, UP, DOWN
	}

	AttkDir atkDir;

	// END OF NEW STUFF 2

	// 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>();

		// NEW 2 Get sword gameobject
		weaponGO = GameObject.Find("Weapon");
		swordGO = GameObject.Find(“Sword");

		swordGO.GetComponent<Renderer>().enabled = false;

	}

	// 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");

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


		// NEW 2 Changed to w and s
		if (Input.GetKey ("w") || Input.GetKey ("s")) {
			if (Input.GetKey ("s")) {

				animator.Play ("WalkDown");

				// NEW 2 Rotate Link's sword
				// weaponGO.transform.rotation = Quaternion.Euler(0, 0, 0);

				// Set attack direction to down and call for animation
				atkDir = AttkDir.DOWN;

			}

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

				animator.Play ("WalkUp");

				// NEW 2 Rotate Link's sword
				// weaponGO.transform.rotation = Quaternion.Euler(0, 0, 180);

				// Set attack direction to up and call for animation
				atkDir = AttkDir.UP;

			}
		}  else {

			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;
				}
				*/

				// NEW 2 Rotate Link's sword
				// weaponGO.transform.rotation = Quaternion.Euler(0, 0, 90);
				// weaponGO.transform.position = new Vector3(transform.position.x, 
				//	transform.position.y, transform.position.z);

				// Set attack direction to right and call for animation
				atkDir = AttkDir.RIGHT;


			}

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

				// NEW 2 Updated by adding WalkLeft
				animator.Play ("WalkLeft");

				// NEW 2 Rotate Link's sword
				//weaponGO.transform.rotation = Quaternion.Euler(0, 0, -90);
				//weaponGO.transform.position = new Vector3(transform.position.x, 
				//	transform.position.y - .55f, transform.position.z);

				// Set attack direction to left and call for animation
				atkDir = AttkDir.LEFT;


				/*
				// 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);

			swordGO.GetComponent<Renderer>().enabled = false;

		}

		// NEW 2 Trigger sword attack with return key
		if (Input.GetKey (KeyCode.Return)) {

			LinkAttack ();

		}

	}

	// NEW STUFF 2
	void LinkAttack(){

		switch (atkDir) {

		case AttkDir.DOWN:
			{
				StartCoroutine (AnimateSword (0f, 0f));
				break;
			}

		case AttkDir.LEFT:
			{
				StartCoroutine (AnimateSword (-90f, -.55f));
				break;
			}

		case AttkDir.RIGHT:
			{
				StartCoroutine (AnimateSword (90f, 0f));
				break;
			}

		case AttkDir.UP:
			{
				StartCoroutine (AnimateSword (180f, 0f));
				break;
			}

		}

		// END OF NEW STUFF 2

	}

	IEnumerator AnimateSword(float rotation, float vertMove){

		// Make sword appear
		swordGO.GetComponent<Renderer>().enabled = true;

		// Rotate sword into position
		weaponGO.transform.rotation = Quaternion.Euler(0, 0, rotation);

		// Move sword vertically
		weaponGO.transform.position = new Vector3(transform.position.x, 
			transform.position.y + vertMove, transform.position.z);

		// Wait a second before hiding sword again
		yield return new WaitForSeconds (.2f);
		swordGO.GetComponent<Renderer>().enabled = false;


	}

}

Leave a Reply

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