As we continue making Zelda, I’ll show you how to make the camera follow Link. We’ll create a script that animates Link smoothly. I’ll show you how to handle collisions between game assets and I’ll demonstrate the usefulness of tags. We’ll also make a Sound Manager we’ll use the play all our sounds and much more.
Like always all of the code follows the video below. Here is a link to the sprite files used.
If you like videos like this consider donating $1, or turn off AdBlocker. Either helps a lot.
[googleplusone]
Code from the Video
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 |
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; // 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"); rb.velocity = new Vector2 (horzMove * speed, vertMove * speed); // NEW STUFF 1 if (Input.GetKey ("w") || Input.GetKey ("w")) { if (Input.GetKey ("s")) { animator.Play ("WalkDown"); } if (Input.GetKey ("w")) { animator.Play ("WalkUp"); } // NEW STUFF 1 } 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; } } 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); } } // NEW void Update(){ if (Input.GetKey (KeyCode.Space)) { animator.SetTrigger ("AttackRight"); Debug.Log ("HELLO"); } } // END OF NEW } |
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 45 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundManager : MonoBehaviour { // Create Empty in the Hierarchy, name it SoundManager // Add Component -> Audio -> Audio Source // Drag sounds into Sounds Folder // Create SoundManager.cs // Drag sounds from folder to SoundManager Inspector // Drag EatingDots to AudioClip in Inspector // Holds the single instance of the SoundManager that // you can access from any script public static SoundManager Instance = null; // Sound clips for Link // 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); } } |
CameraMove.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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraMove : MonoBehaviour { // Put Link here to track his movement public Transform cameraTarget; // How quickly the camera moves towards Link public float cameraSpeed; // Min and max X and Y movements public float minX; public float minY; public float maxX; public float maxY; // Update used when dealing with Rigid Bodies void FixedUpdate(){ // Make sure the camera has a target if (cameraTarget != null) { // Lerp smoothes movement from the starting position // to the targets position var newPos = Vector2.Lerp (transform.position, cameraTarget.position, Time.deltaTime * cameraSpeed); // Define the cameras new postion var vect3 = new Vector3 (newPos.x, newPos.y, -10f); // Clamp gets the cameras x position and clamps // it between the min and max value var clampX = Mathf.Clamp (vect3.x, minX, maxX); var clampY = Mathf.Clamp (vect3.y, minY, maxY); // Move the camera transform.position = new Vector3(clampX, clampY, -10f); } } } |
Leave a Reply