In this tutorial we continue making our Super Mario Clone! We cover Sorting Layers, Animations, Collisions, Controlling Characters, Awake(), Changing Characters Facing Direction, Jumping, Raycasting and Much More.
All of the code follows the video below. Here are all the Images and Sounds used in this series.
If you like videos like this consider donating $1, or simply turn off AdBlock. Either helps a lot 🙂
[googleplusone]
Download all of my Unity game files so far for Pong, Space Invaders, Tetris and Mario
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 Manny here to track his movement public Transform cameraTarget; // How quickly the camera moves towards Manny 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); } } } |
Manny.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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Manny : MonoBehaviour { // Defines Mannys speed horizontally public float speed = 5; // Defines Mannys facing direction public bool facingRight = true; // Jump speed public float jumpSpeed = 5f; // Mannys components private SpriteRenderer sr; private Rigidbody2D rb; private Animator animator; // Will flip depending if on ground bool isJumping = false; // Used to check if Manny is on the ground // You draw a line out away from the sprite a defined // distance and check if it collides with something // If it hits nothing Raycast() returns null private float rayCastLength = 0.005f; // Sprite width and height private float width; private float height; // How long is the jump button held private float jumpButtonPressTime; // Max jump amount public float maxJumpTime = 0.2f; void FixedUpdate(){ // Get horizontal movement -1 Left, or 1 Right float horzMove = Input.GetAxisRaw ("Horizontal"); // Need to get Mannys y Vector2 vect = rb.velocity; // Change x and keep y as is rb.velocity = new Vector2 (horzMove * speed, vect.y); // Set the speed so the right Animation is played animator.SetFloat("Speed", Mathf.Abs(horzMove)); // Makes sure Manny is facing the right direction if (horzMove > 0 && !facingRight) { FlipManny (); } else if (horzMove < 0 && facingRight) { FlipManny (); } // Get vertical movement float vertMove = Input.GetAxis ("Jump"); if (IsOnGround () && isJumping == false) { if (vertMove > 0f) { isJumping = true; } } // If button is held pass max time set // vertical move to 0 if (jumpButtonPressTime > maxJumpTime) { vertMove = 0f; } // If is jumping and we have a valid jump press length // make Manny jump if (isJumping && (jumpButtonPressTime < maxJumpTime)) { rb.velocity = new Vector2 (rb.velocity.x, jumpSpeed); } // If we have moved high enough make Manny fall // Set Mannys Rigidbody 2d Gravity Scale to 2 if (vertMove >= 1f) { jumpButtonPressTime += Time.deltaTime; } else { isJumping = false; jumpButtonPressTime = 0f; } } // Makes sure components have been created when the // game starts void Awake(){ sr = GetComponent<SpriteRenderer> (); animator = GetComponent<Animator> (); rb = GetComponent<Rigidbody2D> (); // Gets Mannys collider width and height and // then adds more to it. Used to raycast to see // if Manny is colliding with anything so we // can jump width = GetComponent<Collider2D> ().bounds.extents.x + 0.1f; height = GetComponent<Collider2D> ().bounds.extents.y + 0.2f; } // When moving in a direction face in that direction void FlipManny(){ // Flip the facing value facingRight = !facingRight; Vector3 scale = transform.localScale; scale.x *= -1; transform.localScale = scale; } public bool IsOnGround(){ // Check if contacting the ground straight down bool groundCheck1 = Physics2D.Raycast (new Vector2 ( transform.position.x, transform.position.y - height), -Vector2.up, rayCastLength); // Check if contacting ground to the right bool groundCheck2 = Physics2D.Raycast (new Vector2 ( transform.position.x + (width - 0.2f), transform.position.y - height), -Vector2.up, rayCastLength); // Check if contacting ground to the left bool groundCheck3 = Physics2D.Raycast (new Vector2 ( transform.position.x - (width - 0.2f), transform.position.y - height), -Vector2.up, rayCastLength); if (groundCheck1 || groundCheck2 || groundCheck3) return true; return false; } // If Manny falls off the screen destroy the game object /* void OnBecameInvisible(){ Debug.Log ("Manny Destroyed"); Destroy (gameObject); } */ } |
Leave a Reply