Question: can you please advise the correct code for the following: Requirements: PacStudent and Cherry Movement Create a script called PacStudentController.cs. In this script, implement PacStudent's
can you please advise the correct code for the following:
Requirements:
PacStudent and Cherry Movement Create a script called "PacStudentController.cs". In this script, implement PacStudent's movement in the following way:
Use a linear lerping/tweening approach to move from one grid position to another. This grid should align with the LevelGenerator.cs levelMap. I.e. PacStudent should be lerping from one pellet position (or empty space) to another at a fixed speed and be frame-rate independent. o You may NOT use external tween libraries (such as DoTween). If these libraries are found in your project structure, you will be severely penalized. o In Update(), gather player input for moving with the W, A, S, and D key to move PacStudent up, left, down, and right respectively. Store the last key that the player pressed in a member variable called "lastInput". Do not override/clear this variable's value unless the player provides more input o In Update(), if PacStudent is not lerping (i.e. was not previously moving or has just reached a grid position), then... Check lastInput and try to move in that direction to the adjacent grid position. If the adjacent grid position from lastInput is walkable, then store lastInput in a member variable called "currentInput" and lerp to the adjacent grid position. If the adjacent grid position from lastInput is not a walkable area (e.g. a wall), then... Check currentInput (see below) and try to move in that direction to the adjacent grid position. If the adjacent grid position from currentInput is walkable, then lerp to the adjacent grid position. If the adjacent grid position from currentInput is not a walkable area (e.g. a wall), then do nothing (PacStudent stops moving). o The Ghost Wall tile acts as a wall for PacStudent and ghosts outside the ghost start zone, but can be passed through from the inside. When the game starts, PacStudent should be facing to the right, awaiting a new player input. o With this setup, PacStudent's movement should feel like the reference example except with a slight delay in reaction time if the player provides input while PacStudent is lerping https://www.google.com/search?q=play+pacman+doodle o The Week 9 Workshop will go over the specifics of the movement and a visualization of the above "algorithm". No, you may not do PacStudent's movement in any other way. No, you may not use Rigidbody physics to handle movements.
Yes, we know it isn't the common way of doing things, that is the point. It is unique and we are checking to see if you can program unique systems without following the thousands of unimaginative Unity tutorials out there that lead to the same uninspired feeling in games. Make sure that PacStudent's movement animations and audio play when they are lerping, and stop when they are not moving o Remember that there are two movement audio clips - one for when PacStudent is eating a pellet (or about to eat one) and one for when they are moving but not about to eat a pellet in the next grid position. Create a custom dust particle effect and play it around PacStudent when they are moving so that it looks like they are running through dirt (or a similar surface) Create a script called "CherryController.cs" to implement the spawning and movement of the bonus cherry sprite created in the previous assessment. o The bonus cherry should spawn 5 seconds after the previous cherry has been destroyed (and after 5 seconds of the scene starting). o It should be instantiated at a random location just outside of the level map but only be visible when inside the bounds of the level (Hint: look into sprite masks). The starting location should be different every time. o It should move in a straight line, via linear lerping, from one side of the screen to the other, passing through the center point of the level and ignoring collisions with walls, ghosts, and pellets. The bonus cherry should be drawn over all other sprites in the level. o If the cherry reaches the other side of the level, outside of camera view, destroy it.
My LevelGenerator script is below:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LevelGenerator : MonoBehaviour { // Start is called before the first frame update [Header("Prefabs")] public GameObject EmptyPrefab; public GameObject OutsideCornerPrefab; public GameObject OutsideWallPrefab; public GameObject InsideCornerPrefab; public GameObject InsideWallPrefab; public GameObject PowerPelletPrefab; public GameObject TJunctionPrefab; public GameObject GhostExitPrefab; public GameObject PelletPrefab;
[Header("Settings")] public float tileSize = 1f;
private int[,] levelMap = { {1,2,2,2,2,2,2,2,2,2,2,2,2,7}, {2,5,5,5,5,5,5,5,5,5,5,5,5,4}, {2,5,3,4,4,3,5,3,4,4,4,3,5,4}, {2,6,4,0,0,4,5,4,0,0,0,4,5,4}, {2,5,3,4,4,3,5,3,4,4,4,3,5,3}, {2,5,5,5,5,5,5,5,5,5,5,5,5,5}, {2,5,3,4,4,3,5,3,3,5,3,4,4,4}, {2,5,3,4,4,3,5,4,4,5,3,4,4,3}, {2,5,5,5,5,5,5,4,4,5,5,5,5,4}, {1,2,2,2,2,1,5,4,3,4,4,3,0,4}, {0,0,0,0,0,2,5,4,3,4,4,3,0,3}, {0,0,0,0,0,2,5,4,4,0,0,0,0,0}, {0,0,0,0,0,2,5,4,4,0,3,4,4,8}, {2,2,2,2,2,1,5,3,3,0,4,0,0,0}, {0,0,0,0,0,0,5,0,0,0,4,0,0,0}, };
void Start() { int rows = levelMap.GetLength(0); int cols = levelMap.GetLength(1);
GenerateLevel(); AdjustCamera(rows, cols); } void GenerateLevel() { GameObject oldLevel = GameObject.Find("Level 01"); if (oldLevel != null) { DestroyImmediate(oldLevel); }
GameObject generatedLevel = new GameObject("GeneratedLevel");
int rows = levelMap.GetLength(0); int cols = levelMap.GetLength(1);
for (int y = 0; y
GameObject prefabToSpawn = null; float rotation = 0f;
switch (tile) { case 0: prefabToSpawn = EmptyPrefab; break; case 1: prefabToSpawn = OutsideCornerPrefab; rotation = 0f; break; case 2: prefabToSpawn = OutsideWallPrefab; break; case 3: prefabToSpawn = InsideCornerPrefab; break; case 4: prefabToSpawn = InsideWallPrefab; break; case 5: prefabToSpawn = PelletPrefab; break; case 6: prefabToSpawn = PowerPelletPrefab; break; case 7: prefabToSpawn = TJunctionPrefab; break; case 8: prefabToSpawn = GhostExitPrefab; break; }
if (prefabToSpawn != null) { GameObject obj = Instantiate(prefabToSpawn, position, Quaternion.Euler(0,0, rotation)); obj.transform.parent = generatedLevel.transform; } } }
MirrorQuadrants(generatedLevel); }
void MirrorQuadrants(GameObject parent) { int rows = levelMap.GetLength(0); int cols = levelMap.GetLength(1);
Vector3 offset = new Vector3(cols * tileSize, rows * tileSize, 0);
GameObject mirrorH = Instantiate(parent, Vector3.zero, Quaternion.identity); mirrorH.name = "Mirror_H"; mirrorH.transform.localScale = new Vector3(-1,1,1);
GameObject mirrorV = Instantiate(parent, Vector3.zero, Quaternion.identity); mirrorV.name = "Mirror_V"; mirrorV.transform.localScale = new Vector3(1, -1, 1);
GameObject mirrorHV = Instantiate(parent, Vector3.zero, Quaternion.identity); mirrorHV.name = "Mirror_HV"; mirrorHV.transform.localScale = new Vector3(-1,-1,1); }
void AdjustCamera(int rows, int cols) { Camera cam = Camera.main;
if (cam != null) { cam.orthographic = true; cam.orthographicSize = Mathf.Max(rows, cols) / 1.5f; cam.transform.position = new Vector3(cols / 2f, rows / 2f, -10f); } else { Debug.LogWarning("Main Camera not found! Please tag your camera as 'MainCamera'."); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
