Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Unity3D realize the classic Mini Game Pacman

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how Unity3D implements the classic Mini Game Pacman. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Project overview overall layout

Map introduction

In addition to sound effects, the elements on the game map are:

Ordinary jelly beans (players will get points by moving, passing ordinary jelly beans will be eaten)

Special jelly beans (after players eat, you can make all enemies stop moving, resulting in a phantom effect)

Separation wall (a wall equivalent to a labyrinth in which the gap between two walls can be moved)

Remaining game time Remain (a total of 300s, when the time is up, the game is over)

Game time spent so far Now (set between 0,300)

Game score Score so far (the higher the better)

Enemy man-machine (divided into four colors, small orange, small pink, small blue, small red)

Our players (Xiao Huang, one of the main players of the game)

Introduction to how to play

How to play (the judgment of winning or losing):

Try to eat all the jelly beans within the set time limit and not be touched by the enemy machine.

If it's all finished, it's better to use it in less time.

If you haven't finished eating, the one with the highest score at the same time is better.

Eating special jelly beans (big jelly beans) can make all enemies stop moving, resulting in a phantom effect, which is equivalent to the reaction of the invincible effect, so that they will not be attacked for a certain period of time, prolonging the player's survival time. The player's survival rate can be changed through probability design.

Related knowledge

In the process of learning, several important knowledge points are: preform, clone, sprite renderer, rendering level, script, collision detection, trigger detection, AI design, UI design, fixed physical frame.

Version description

Shrimp uses a newer version of Unity-2021.1.16. Here, friends are advised to download the same version as the tutorial, otherwise they may encounter a bit of emotional consumption.

It can be installed in Unity Hub as I do, and Hub feels good to use.

Project source code

Player's Mobility (PacmanMove.cs)

Using UnityEngine; public class PacmanMove: MonoBehaviour {/ / the moving speed of Pac-Man public float speed = 0.35f; / / the destination where Pac-Man will go next time private Vector2 dest = Vector2.zero; private void Start () {/ / guarantee that Pac-Man will not move dest = transform.position at the beginning of the game. } private void FixedUpdate () {/ / interpolate to get the next movement coordinate Vector2 temp = Vector2.MoveTowards (transform.position, dest, speed) to be moved to the dest position; / / with each call to the frame, keep moving to the right to take the middle value / / set the position of the object through the rigid body GetComponent () .MovePosition (temp) / / you must reach the location of the previous dest before issuing a new destination setting instruction if ((Vector2) transform.position = = dest) {if ((Input.GetKey (KeyCode.UpArrow) | | Input.GetKey (KeyCode.W)) & & Valid (Vector2.up)) {dest = (Vector2) transform.position + Vector2.up } if ((Input.GetKey (KeyCode.DownArrow) | | Input.GetKey (KeyCode.S)) & & Valid (Vector2.down)) {dest = (Vector2) transform.position + Vector2.down } if ((Input.GetKey (KeyCode.LeftArrow) | | Input.GetKey (KeyCode.A)) & & Valid (Vector2.left)) {dest = (Vector2) transform.position + Vector2.left } if ((Input.GetKey (KeyCode.RightArrow) | | Input.GetKey (KeyCode.D)) & & Valid (Vector2.right)) {dest = (Vector2) transform.position + Vector2.right;} / / get the moving direction Vector2 dir = dest-(Vector2) transform.position / / set the acquired movement direction to the animated state machine GetComponent (). SetFloat ("DirX", dir.x); GetComponent (). SetFloat ("DirY", dir.y);}} / / detect whether the location you are going to can reach private bool Valid (Vector2 dir) {/ / record the current position Vector2 pos = transform.position / / emit a ray from the position to be reached to the current position, and store the ray information RaycastHit2D hit = Physics2D.Linecast (pos + dir, pos); / / return whether the ray hit the collider return on Pac-Man itself (hit.collider = = GetComponent ());}}

Jelly bean design (Pacdot.cs)

Using UnityEngine; public class Pacdot: MonoBehaviour {public bool isSuperPacdot = false; private void OnTriggerEnter2D (Collider2D collision) {if (collision.gameObject.name = = "Pacman") {if (isSuperPacdot) {GameManager.Instance.OnEatPacdot (gameObject); GameManager.Instance.OnEatSuperPacdot (); Destroy (gameObject) } else {GameManager.Instance.OnEatPacdot (gameObject); Destroy (gameObject);}

Enemy man-machine movement (GhostMove)

Using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement; public class GhostMove: MonoBehaviour {public GameObject [] wayPointsGos; public float speed = 0.2f; private List wayPoints = new List (); private int index = 0; private Vector3 startPos; private void Start () {startPos = transform.position + new Vector3 (0,3,0); LoadAPath (wayPointsGos [GameManager.Instance.usingIndex [GetComponent (). SortingOrder-2]) } private void FixedUpdate () {if (transform.position! = wayPoints [index]) {Vector2 temp = Vector2.MoveTowards (transform.position, wayPoints [index], speed); GetComponent () .MovePosition (temp);} else {index++; if (index > = wayPoints.Count) {index = 0 LoadAPath (wayPointsGos [Random.range (0, wayPointsGos.Length)]);}} Vector2 dir = wayPoints [index]-transform.position; GetComponent (). SetFloat ("DirX", dir.x); GetComponent () .SetFloat ("DirY", dir.y);} private void LoadAPath (GameObject go) {wayPoints.Clear () Foreach (Transform t in go.transform) {wayPoints.Add (t.position);} wayPoints.Insert (0, startPos); wayPoints.Add (startPos) } private void OnTriggerEnter2D (Collider2D collision) {if (collision.gameObject.name = = "Pacman") {if (GameManager.Instance.isSuperPacman) {transform.position = startPos-new Vector3 (0,3,0); index = 0; GameManager.Instance.score + = 500 } else {collision.gameObject.SetActive (false); GameManager.Instance.gamePanel.SetActive (false); Instantiate (GameManager.Instance.gameoverPrefab); Invoke ("ReStart", 3f);}} private void ReStart () {SceneManager.LoadScene (0) }} the above is how Unity3D implements the classic Mini Game Pacman. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report