Unity 2D CHECKPOINTS Tutorial (Simple and Easy!) | Unity 2D Platformer Tutorial #9

Rehope Games
21 Dec 202205:39

Summary

TLDRIn this engaging video, the creator guides viewers through the process of implementing a checkpoint system in a game. Starting with scene modifications, including a new background color, moving platforms, and duplicated obstacles, the tutorial then demonstrates how to respawn players from the beginning upon collision with obstacles. The main focus is on adding checkpoints with custom images, using box colliders with the 'Is Trigger' feature, and scripting to update the player's respawn position. The tutorial also covers spawning the player slightly above the checkpoint, changing checkpoint images upon activation, and disabling checkpoints after interaction to prevent multiple respawns. Finally, the creator shows how to create a prefab for easy reuse of the checkpoint system, concluding with a call to action for viewers to subscribe for more content.

Takeaways

  • 🎮 The video is part of a game making series focusing on implementing a Checkpoint System.
  • 🛠️ The scene for the video has been updated with a new background color, moving platforms, and duplicated obstacle objects.
  • 🔄 The player respawns from the starting point when hitting an obstacle, as previously implemented in the respawn system.
  • 📍 Two images have been created for the checkpoint and added to the project.
  • 📐 A box collider with the 'Is Trigger' feature is added to the checkpoint object for interaction.
  • 📝 A checkpoint script is created to manage the game's checkpoint logic.
  • 🔄 The 'startPos' variable in the GameController script is renamed to 'checkpointPos' to store the last checkpoint position.
  • 🔄 An 'UpdateCheckpoint' method is introduced to update the 'checkpointPos' when the player reaches a checkpoint.
  • 🔝 An empty 'respawn point' object is created to determine the exact spawn location above the checkpoint.
  • 🖼️ The checkpoint image changes upon activation to provide visual feedback.
  • 🔄 Additional checkpoints can be added by duplicating existing ones, allowing the player to respawn from the last checkpoint reached.
  • ❌ The checkpoint's collider is disabled after interaction to prevent multiple interactions with the same checkpoint.
  • 🛠️ The checkpoint object is turned into a prefab for easy reuse in the game development process.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to create a Checkpoint System for a game.

  • What changes were made to the scene for this tutorial?

    -The changes made to the scene include changing the background color, adding a moving platform, and duplicating the obstacle object.

  • What happens to the player when they hit an obstacle in the current system?

    -In the current system, when the player hits an obstacle, they respawn from the starting point.

  • What is the purpose of adding a checkpoint to the game?

    -The purpose of adding a checkpoint is to allow the player to respawn at a specific point in the game after they die, rather than starting over from the beginning.

  • How many checkpoint images were created and added to the project?

    -Two checkpoint images were created and added to the project.

  • What is the role of the box collider added to the checkpoint object?

    -The box collider is added to the checkpoint object to detect when the player reaches the checkpoint, with the 'isTrigger' feature enabled to handle the interaction without physical collision.

  • What variable is renamed in the GameController script and what is its purpose?

    -The variable 'startPos' is renamed to 'checkpointPos' to store the position of the checkpoint, which the player will respawn at if they die before reaching any checkpoint.

  • What is the purpose of the 'UpdateCheckpoint' method in the GameController script?

    -The 'UpdateCheckpoint' method is used to update the position of the 'checkpointPos' variable when the player reaches a new checkpoint.

  • Why is an empty object named 'respawn point' created in the checkpoint?

    -An empty object named 'respawn point' is created to determine the exact height where the player should spawn above the checkpoint.

  • How does the checkpoint image change when activated?

    -The checkpoint image changes when activated by dragging and dropping the new images into their respective slots in the checkpoint script.

  • What is the solution if a player touches a previous checkpoint and wants to respawn from the last checkpoint instead?

    -The solution is to disable the collider of the checkpoint once it makes contact with the player, preventing it from interacting with the player a second time.

  • What is the advantage of creating a prefab from the checkpoint object?

    -Creating a prefab from the checkpoint object allows for easy reuse and placement of the checkpoint in the game wherever and whenever needed.

Outlines

00:00

🛠️ Implementing a Checkpoint System in Game Development

This paragraph outlines the process of creating a checkpoint system in a game development series. The video begins with an introduction to the task and the changes made to the game scene, such as altering the background color, adding a moving platform, and duplicating obstacle objects. The current respawn system is discussed, which respawns the player from the starting point upon death. The tutorial then proceeds to add checkpoints to the game, introducing two checkpoint images and detailing the steps to add a box collider with the 'isTrigger' feature to the checkpoint object. A checkpoint script is created to update the player's respawn position upon reaching a checkpoint. The script involves renaming the 'startPos' variable to 'checkpointPos' and creating a method 'UpdateCheckpoint' to change the respawn position when a checkpoint is reached. The player's interaction with the checkpoint is tested in the game, demonstrating that the player respawns at the checkpoint after passing it. Additional features like adjusting the spawn height and changing the checkpoint image upon activation are also covered. The paragraph concludes with the creation of a prefab from the checkpoint object for easy reuse.

05:10

📦 Creating a Prefab for Reusable Checkpoints

In this paragraph, the focus is on streamlining the game development process by creating a prefab from the checkpoint object. The speaker explains the benefits of using a prefab, which allows for easy and efficient use of the checkpoint system in different parts of the game or in future projects. The process involves duplicating the checkpoint and testing the game to ensure that the player respawns correctly from the last checkpoint reached. The tutorial also addresses how to prevent the player from respawning at an earlier checkpoint after reaching a later one by disabling the collider of the checkpoint once it has been activated. The paragraph wraps up with a demonstration of the checkpoint system in action, showing the player respawning at the first and second checkpoints and then from the last checkpoint after touching an earlier one again. The creation of a prefab simplifies the process of implementing checkpoints, making it a valuable tool for game developers.

Mindmap

Keywords

💡Checkpoint System

A checkpoint system in game development refers to a mechanism that allows a player to save their progress within a game, enabling them to resume from a specific point if they fail or quit the game. In the context of this video, the checkpoint system is a core feature being implemented, allowing the player to respawn at the last reached checkpoint instead of starting over from the beginning. This system enhances the gaming experience by providing a sense of progress and reducing frustration.

💡Respawn

Respawn is a term used in gaming to describe the process where a player's character is brought back to life or reappears at a certain point after being defeated or dying. In the video, the respawn system is initially set up to return the player to the starting point upon death. However, the goal is to modify this so that the player can respawn at the last checkpoint reached, which is a more advanced and player-friendly feature.

💡Collider

In game development, a collider is a component used to define the boundaries of an object for the purpose of collision detection. It allows the game engine to determine when objects interact with each other. In the video, the creator adds a box collider to the checkpoint object, sets its size, and enables the 'isTrigger' feature to detect when the player character enters the checkpoint area without causing a physical collision.

💡Game Controller Script

The game controller script is a piece of code that manages the overall game logic, including player movement, game states, and interactions with other game objects. In the video, the script is modified to include a 'checkpointPos' variable that tracks the last checkpoint the player has reached, which is then used to determine the respawn point after the player dies.

💡UpdateCheckpoint Method

The UpdateCheckpoint method is a function within the game controller script that updates the position of the checkpointPos variable to the location of the checkpoint the player has just reached. This method is called when the player interacts with a checkpoint, ensuring that the game knows where the player should respawn if they die after passing that checkpoint.

💡IsTrigger Feature

The 'isTrigger' feature in Unity is a property of a collider that, when enabled, allows the collider to detect when objects enter or exit its boundaries without causing a physical reaction, such as a bounce or push. In the video, the checkpoint's collider uses this feature to detect the player's arrival without affecting the player's movement, thus triggering the checkpoint activation.

💡Respawn Point

A respawn point is a designated location in a game where a player's character reappears after death or when restarting a level. In the context of the video, the creator creates an empty object named 'respawn point' above the checkpoint image to control where the player will reappear after respawning. This adds a layer of control over the player's starting position upon respawn.

💡Transform Variable

In Unity, a Transform variable is used to store and manipulate the position, rotation, and scale of game objects in 3D space. In the video, the creator uses a Transform variable named 'respawnPoint' to reference the empty object created for the respawn location, ensuring that the player spawns at the correct height above the checkpoint.

💡Image Change on Activation

This concept refers to the visual feedback provided to the player when a checkpoint is activated. In the video, the creator wants the checkpoint image to change as soon as it is reached, serving as a clear indication to the player that the checkpoint has been successfully activated and can be used as a respawn point.

💡Duplicating Checkpoints

Duplicating checkpoints in the video involves creating additional checkpoint objects in the game scene by copying an existing checkpoint. This is done using the shortcut CTRL+D, and it allows for the quick setup of multiple checkpoints throughout the game level, enhancing the game's structure and the player's ability to progress and respawn at different points.

💡Prefab

A prefab in Unity is a reusable game object or component that can be easily instantiated in the game scene multiple times. Prefabs are used to create consistent objects without having to recreate them from scratch. In the video, the creator turns the checkpoint object into a prefab, which simplifies the process of adding more checkpoints to the game and ensures consistency across all checkpoint instances.

Highlights

Introduction to the game making series and the goal of implementing a Checkpoint System.

Scene modifications including a new background color, moving platform, and duplicated obstacle objects.

Demonstration of the player respawning from the starting point upon hitting an obstacle.

Introduction of the checkpoint concept to improve the respawn system.

Use of two images for the checkpoint and their integration into the project.

Adding a box collider to the checkpoint object with an enabled 'isTrigger' feature.

Creation of a checkpoint script to manage game state.

Modification of the GameController script to handle checkpoint positions.

Implementation of the UpdateCheckpoint method to change the respawn position.

Linking the checkpoint script to the GameController for dynamic respawn points.

Adjusting the player's spawn point to be above the checkpoint image.

Adding an empty object named 'respawn point' to control the exact spawn location.

Changing the checkpoint image upon activation for visual feedback.

Duplicating checkpoints for extended game levels.

Ensuring the player respawns at the last reached checkpoint.

Disabling the checkpoint collider after interaction to prevent multiple interactions.

Creating a prefab from the checkpoint for easy reuse in the game.

Encouragement to subscribe for more video content and a closing statement.

Transcripts

play00:00

Hello everyone,

play00:01

Let's continue the game making series.

play00:03

In this video we will make Checkpoint System.

play00:06

All right! Let’s get started.

play00:09

I made a few changes to the scene for this video. 

play00:12

I changed the background color,

play00:14

added moving platform

play00:15

and duplicated the obstacle object,

play00:17

just that's it.

play00:19

Let’s start the game in this case.

play00:21

As you can see,

play00:22

the player respawns from the starting point

play00:25

when hit the obstacle.

play00:26

we did the respawn system in the previous video.

play00:29

No matter how far the player goes,

play00:31

when it die, it is respawned from the beginning.

play00:34

Let’s add checkpoint to the scene.

play00:36

I made 2 images for checkpoint

play00:38

and added them to this project.

play00:41

I’ll drag and drop the  checkpoint image onto the scene. 

play00:47

And I’m gonna add box collider  to the checkpoint object. 

play00:51

set the size of the collider .

play00:58

and then I’ll enable istrigger feature of the collider. 

play01:03

I’m gonna create checkpoint script.

play01:07

Open the checkpoint script.

play01:10

First of all, I will make a small  addition to the game controller script . 

play01:14

As you can see, when the player dies,  they only respawn from the starting point. 

play01:19

I will rename the startPos  variable to checkpointPos. 

play01:22

checkpointPos will be equal to the player's starting position

play01:26

when the game first starts.

play01:27

If the player dies before reaching any checkpoint,

play01:30

it will respawn from the starting point.

play01:33

I will create a new method  called UpdateCheckpoint. 

play01:36

when the player reaches any checkpoint,

play01:38

I will call this method from checkpoint

play01:40

script and i'll change the position  of the checkpointPos variable. 

play01:45

Alright!

play01:46

Save this script and  go back to checkpoint script.

play01:50

I’ll create a variable named gameController

play01:52

to access the GameController script.

play01:55

if the player touches the checkpoint,

play01:57

I will  update the position of the checkpointPos variable

play02:00

to the position of the touched checkpoint.

play02:03

ok! save the script and go back to unity.

play02:07

Start the game.

play02:10

if we die before reaching the checkpoint,

play02:12

we spawn from the starting point.

play02:15

When we die after reaching the checkpoint,

play02:17

we are respawn at the checkpoint.

play02:21

I want the player to spawn slightly  above the checkpoint image. 

play02:27

Click on the checkpoint object and create  an empty object named respawn point. 

play02:34

i raise this empty object to the  height i want the player to spawn. 

play02:38

Open the checkpoint script. I’ll create a Transform variable  

play02:42

named respawnPoint.

play02:51

Go back to unity.

play02:52

Drag and drop respawnPoint object to this slot.

play02:57

Start the game.

play03:01

As you can see, the player now  spawns above the checkpoint. 

play03:04

I want its image to change when  the checkpoint is activated. 

play03:09

If you want, you can add animation or do different things,

play03:12

it's completely up to your taste.

play03:20

drag and drop the images into the their slots. 

play03:29

Nice !

play03:30

As soon as the checkpoint  is activated, its image changed.

play03:35

i want to add one more checkpoint to the scene,  

play03:38

so i will duplicate this  checkpoint with CTRL+D shortcut. 

play03:49

Start the game. 

play03:56

as you can see the player  respawns from the last checkpoint. 

play04:03

if the player touches the previous checkpoint  again,

play04:06

it spawns from the previous checkpoint.

play04:10

If you don't want this to  happen, we can simply solve it. 

play04:14

Go back to checkpoint script.

play04:16

I'll access the collider of the checkpoint.

play04:18

As soon as the checkpoint makes contact with the player,

play04:21

I will disable its collider.

play04:23

so it will not be able to interact  with the player a second time. 

play04:28

Save the script and Let’s test it. 

play04:33

I reach the first checkpoint, died  and respawn from the first checkpoint. 

play04:39

I reach the second checkpoint, died  and respawn from the second checkpoint. 

play04:48

I touched the first checkpoint again,

play04:50

died and respawn from the last checkpoint.

play04:54

Nice! 

play05:10

I'll create a prefab from the checkpoint object. 

play05:14

so I can easily use the checkpoint  whenever and wherever I want.

play05:27

I hope the video useful for you.

play05:29

Please don’t forget to Subscribe for more videos.

play05:31

Thanks for watching.

play05:32

See you in the next video.

Rate This

5.0 / 5 (0 votes)

相关标签
Game DevelopmentUnity TutorialCheckpoint SystemRespawn MechanicsGame DesignProgrammingGameplay FeaturesVideo SeriesInteractive TutorialUnity Engine
您是否需要英文摘要?