In 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
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 |
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
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 |
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