Surti99 / PacmanGrid_Tutorial

This repository hosts a meticulously crafted project that replicates the iconic and nostalgic arcade game, Pac-Man, utilizing the robust and versatile Unity game development engine. This clone aims to pay homage to the original game while providing a foundation for further exploration and experimentation in game development.
0 stars 0 forks source link

Making the Ghost work #3

Open Surti99 opened 11 months ago

Surti99 commented 11 months ago

Create a Base Object:

Start by creating a new GameObject for the base of your ghost variants. Name it "Ghost Base."

Layer Setup: Assign the "Ghost" layer to the Ghost Base GameObject. This helps in managing the rendering order of the ghosts. Rigidbody2D Component:

Add a Rigidbody2D component to the Ghost Base. This component allows for physics interactions and movement control for the ghosts.

Separate Body and Eyes: For better rendering control, it's a good practice to separate the ghost's body and eyes. In Unity's 2D system, the Z-position determines rendering order. By separating them, you can ensure that the eyes render above the body. Child Objects:

Inside the Ghost Base GameObject, create the following child objects: Body: This child object represents the ghost's main body. Eyes: This child object represents the ghost's eyes, which can move independently. Blue: If your ghost has a blue variant (for example, when it's in a frightened state), create a child object for it. White: Similarly, if your ghost has a white variant (for example, when it's eaten by Pacman), create a child object for it. Sprite Animations:

Add the Animated Sprite component to the child objects (Body, Eyes, Blue, White) that need animation. Populate the Animated Sprite component with the different sprites that you want to loop through for animation. Ghost Script:

Attach the Ghost script to the Ghost Base GameObject. This script manages the behavior and movement of the ghosts. Create Prefab Variants:

Once you've set up the Ghost Base GameObject with all its components, drag it into the Project view. This action creates a prefab with the default ghost object. Creating Different Colored Ghosts:

To create variants of the ghost with different colors, drag the Ghost Base prefab from the Project view into the scene. Modify the appearance and properties of the ghost in the scene (change sprite colors, animations, etc.). Save as Prefab Variant:

After customizing the ghost's appearance in the scene, drag the modified ghost back into the Project view. This creates a new prefab variant. You can repeat this process to make as many different colored ghost variants as needed.

Surti99 commented 11 months ago

After you create all the ghost we have to make all the scripts. We have made 4 scripts for the ghost to operate properly.

Ghost GhostBehaviour GhostScatter GhostHome GhostFrightend


Surti99 commented 11 months ago

GHOST SCRIPT

Attributes and Dependencies:

The Ghost class has been decorated with [DefaultExecutionOrder(-10)] to ensure its execution priority in the Unity's script order. Required the Movement component to be attached to the same GameObject, ensuring the ghost has the necessary movement capabilities. Component References:

Integrated references to multiple ghost behavior components (GhostHome, GhostScatter, GhostChase, and GhostFrightened) which will dictate how the ghost acts in various game situations. Set an initialBehavior which the ghost will adhere to at the start of the game. Initialization:

In the Awake method, each component reference is initialized by getting the corresponding component from the GameObject. Starting Behavior:

The Start method ensures the ghost begins with the proper behavior and state by calling the ResetState method. State Management:

Introduced a ResetState method to: Activate the ghost in the scene. Reset its movement state. Set its behaviors (e.g., disabling frightened and chase, enabling scatter). Enable its initialBehavior if it's set and is different from home. Positioning Utility:

The SetPosition method updates the ghost's position while maintaining its z-coordinate to preserve rendering depth. Collision Handling:

Implemented the OnCollisionEnter2D method to detect collisions. Checks if the ghost collides with Pacman: If the ghost is in the frightened state, it registers as being eaten by Pacman. Otherwise, it registers Pacman as being eaten by the ghost.

Surti99 commented 11 months ago

GHOSTSCATTER SCRIPT Inheritance:

The GhostScatter class is derived from the GhostBehavior base class, implying it shares common properties and methods established in GhostBehavior. Transition to Chase Mode:

The OnDisable method ensures that when the GhostScatter behavior is disabled, the ghost automatically switches to its chase mode. Node Collision Handling:

The OnTriggerEnter2D method is used to manage the ghost's behavior when it encounters nodes in the maze. Upon collision with a node and provided the ghost is not in its frightened state: The ghost chooses a random direction from the available directions linked to that node. The system is designed to prevent the ghost from immediately reversing its current direction. If the chosen direction is directly opposite to its current movement direction, the ghost will increment to the next available direction. If this increment exceeds the list of available directions, it wraps around to the start, ensuring continuous movement. Finally, the chosen direction is set for the ghost's movement.

Surti99 commented 11 months ago

GHOSTHOME SCRIPT Inheritance:

The GhostHome class is derived from the GhostBehavior base class, implying it shares common properties and methods from GhostBehavior. Defined Positions:

Public Transform variables inside and outside specify the inside and outside positions of the ghost's home. Behavior Enablement:

Upon enabling this behavior, all active coroutines are halted using the StopAllCoroutines method. This ensures a consistent behavior transition. Behavior Disablement:

When disabling this behavior, an exit transition coroutine ExitTransition is initiated if the GameObject is active in the hierarchy. This transition oversees the ghost's exit from its home. Handling Wall Collisions:

The OnCollisionEnter2D method detects collisions. If the ghost collides with an obstacle layer (e.g., a wall), its direction is reversed, creating an effect where the ghost appears to be bouncing inside its home. Exit Transition Animation:

The ExitTransition coroutine is responsible for animating the ghost's movement from inside its home to the outside. The ghost's movement component is temporarily made kinematic and disabled, giving control to the coroutine for manual position adjustments. Initially, the ghost is moved to the specified inside position. Following that, it moves towards the outside position. After exiting the home, a random left or right direction is chosen for the ghost, and its movement component is re-enabled and set to non-kinematic.