In the next 2 videos I’ll cover how to lock Ms. Pac-Man perfectly inside of the maze as she moves around. We will do this not through colliders, but instead by using pivot points, which will make the movement very clean and also resource light. We’ll cover how to Setup Pivot Points, Putting GameObjects in an Array, Selecting GameObjects in Code, Cycling through GameObjects in Empties, Moving Ms. Pac-Man, Animating Ms. Pac-Man and much more.
The images are here and the code follows the video below.
If you like videos like this, consider donating $1 on Patreon, or simply turn off AdBlocker.
[googleplusone]
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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Gameboard : MonoBehaviour { // Create array that holds all TurningPoints public Transform[,] gBPoints = new Transform[27,30]; // References the empty that contains all the points private GameObject turningPoints; // Use this for initialization void Start () { // Get the Empty named TurningPoints turningPoints = GameObject.Find("TurningPoints"); // Cycle through each point in that empty foreach (Transform point in turningPoints.transform) { // Get vector position of point Vector2 pos = point.position; // Put point in the array gBPoints[(int)pos.x, (int)pos.y] = point; } } } |
TurningPoint.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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TurningPoint : MonoBehaviour { public TurningPoint[] nextPoints; public Vector2[] vectToNextPoint; // Use this for initialization void Start () { // Initialize the array that will hold the vector to // each nearby TurningPoint vectToNextPoint = new Vector2[nextPoints.Length]; for (int i = 0; i < nextPoints.Length; i++) { // Get each point so we can find its position // in relation to the current TurningPoint TurningPoint nextPoint = nextPoints [i]; // Get the Vector to the next TurningPoint // Returns (1, 0) for right, (0, -1) for down, etc. Vector2 pointVect = nextPoint.transform.localPosition - transform.localPosition; // Store vector to Vector2 array // Without normalized the values wouldn't be 0, 1, or -1 vectToNextPoint[i] = pointVect.normalized; } } } |
MsPacman.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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; // Used to find out if Pac-Man hits a wall using System; public class MsPacman : MonoBehaviour { public float speed = 0.4f; // Used to move Ms. Pacman private Rigidbody2D rb; // 5. Sprite used when Pac-Man is paused public Sprite pausedSprite; // Makes sure components have been created when the // game starts void Awake(){ // Get Pac-man Rigidbody rb = GetComponent<Rigidbody2D> (); } void Start(){ rb.velocity = new Vector2 (-1, 0) * speed; } void FixedUpdate(){ float horzMove = Input.GetAxisRaw ("Horizontal"); float vertMove = Input.GetAxisRaw ("Vertical"); // Move Left if (Input.GetKeyDown ("a")) { // Changes the direction to left rb.velocity = new Vector2 (horzMove, 0) * speed; // Faces Pacman left transform.localScale = new Vector2 (1, 1); // Sets rotation to default transform.localRotation = Quaternion.Euler (0, 0, 0); } else if (Input.GetKeyDown ("d")) { // Changes the direction to right rb.velocity = new Vector2 (horzMove, 0) * speed; // Faces Pacman right transform.localScale = new Vector2 (-1, 1); // Sets rotation to default transform.localRotation = Quaternion.Euler (0, 0, 0); } else if (Input.GetKeyDown ("w")){ // Move up rb.velocity = new Vector2 (0, vertMove) * speed; // Sets facing direction to default transform.localScale = new Vector2 (1, 1); // Rotate facing up transform.localRotation = Quaternion.Euler (0, 0, 270); } else if (Input.GetKeyDown ("s")){ // Move Down rb.velocity = new Vector2 (0, vertMove) * speed; // Sets facing direction to default transform.localScale = new Vector2 (1, 1); // Rotate facing down transform.localRotation = Quaternion.Euler (0, 0, 90); } } } |
Leave a Reply