In 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
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
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