UE-Simple Game

 Learning Content 

  • Continuously move the player forward
  • Generates obstacles that the player must avoid
  • Randomize barriers to create variation
  • Create a restart button that is displayed when the player encounters an obstacle

 

getting Started

Download the startup project and unzip it. Go to the project folder and open InfiniteMatrix.uproject .

NOTE: If you get a message that the project was created with an earlier version of Unreal Editor, that's okay (the engine is updated frequently). You can choose the option to open a copy, or the option to convert in-place.

Press Play to test motion controls. You can move vertically and horizontally by moving the mouse.

The first thing you want to do is keep the player moving forward.

move the character forward

Navigate to the Blueprints folder and open BP_Player .

To move the Player forward, you add an offset to the Player's position each frame.

First, you need to create a variable that defines how fast the player moves forward. Create a Float variable called ForwardSpeed ​​and set its default value to 2000 .

Next, make sure you're in the Event Graph, and find the Event Tick node. Create the following settings:

By multiplying ForwardSpeed ​​with Delta Seconds, you can get framerate independent results .

Note: If you're new to framerate independence, please read our Blueprints tutorial. We'll cover it in the "Framerate Independence" section.

Next, you'll use this result to move the player along a single axis.

move along a single axis

To move the Player, create an AddActorWorldOffset node. Set Sweep to true by left clicking on the checkbox .

If you try to connect a Float result to a Delta Location input, Unreal will automatically convert it to a Vector .

However, this puts Float values ​​into the Vector's X, Y, and Z components. For this game, forward movement should only be along the X axis . Fortunately, you can split the Vector into three Float components .

Make sure the Delta Location pin of the AddActorWorldOffset node is not connected . Right click on the Delta Location pin and select Split Struct Pin .

Finally, wire everything up like this:

Let's recap:

  1. Every frame, the game multiplies ForwardSpeed ​​and Delta Seconds to get a framerate independent result
  2. AddActorWorldOffset will use the result to move the player along the X axis
  3. Since Sweep is enabled, the player will stop moving forward if there is anything in the way

Click Compile and return to the main editor. If you press Play , you will go through the tunnel.

Instead of manually placing tunnels, you can create blueprints that automatically generate tunnels.

Create tunnel builder

Go to Content Browser and make sure you are in the Blueprints folder. Create a new Blueprint class with Actor as parent . Name it BP_TunnelSpawner , and turn it on.

Since the game is constantly spawning tunnels, it's a good idea to create a spawn function . Go to the My Blueprint panel and create a new function called SpawnTunnel . The purpose of this function is to generate a tunnel at the provided location.

To pass a location to a function, the function requires an input parameter . These will appear as input pins when you call the function.

They will also appear as output pins on the function's Entry node.

Let's go ahead and create an input parameter. Make sure you are on the graph for the SpawnTunnel function. Select the Entry node and go to the Details panel. Click on the + sign next to the input section .

Rename the input parameter to SpawnLocation and change its type to Vector .

To spawn a tunnel, add a Spawn Actor From Class node. Click the drop-down menu located to the right of the Class pin and select BP_Tunnel .

To set the spawn location, right-click on the Spawn Transform pin and select Split Struct Pin . Afterwards, link the Spawn Actor From Class node to the Entry node as follows:

Now, whenever the SpawnTunnel function is called, it will spawn a BP_Tunnel instance at the provided location .

Let's test it out!

test tunnel generator

Switch to the Event Graph and find the Event BeginPlay node. Add a SpawnTunnel node and connect it to the Event BeginPlay node.

On the SpawnTunnel node, set the Spawn Location to (2000, 0, 500) .

Now, when the game starts, it spawns a tunnel above the player. Click Compile and return to the main editor.

First, remove BP_Tunnel from the level . Do this by left clicking on the BP_Tunnel in the World Outliner . Then, press the Delete key to remove it from the level.

Next, go to the Content Browser. Left click and drag the BP_TunnelSpawner into  the viewport. This will add an instance of it to the level.

If you press Play , the game will spawn a tunnel above and away from the player.

After testing, return to BP_TunnelSpawner . Reset the Spawn Location of the SpawnTunnel node to (0, 0, 0).

After that, click compile and return to the main editor.

In the next section, you will set up the functionality for BP_Tunnel .

Set up the tunnel blueprint

BP_Tunnel will be responsible for two things. The first is to detect when the game should spawn a new tunnel. To do this, you will create a trigger zone. Once triggered, BP_Tunnel will tell BP_TunnelSpawner to spawn a new tunnel. By doing this, you can create the illusion of an endless tunnel.

The second thing it does is define a spawn point. BP_TunnelSpawner will use this point as the next spawn location.

Let's start by creating the trigger area.

create trigger zone

Open BP_Tunnel and go to the Components panel. Add a Box Collision component and name it TriggerZone .

Currently the collision area is small. Go to the details panel and find the Shape section. Set the Box Extent property to (32, 500, 500).

Next, set the Location property to (2532, 0, 0) . This will place the TriggerZone at the end of the tunnel mesh. This means that new tunnels will only be generated when the player reaches the end of the tunnel.

Now, it's time to create the spawn point

create spawn point

To define where the spawn point is, you can use the Scene component. These components are great for defining positions since they only contain a transform. They are also visible in the viewport, so you can see where the spawn point is.

Go to the Components panel and make sure you have nothing selected. Add a Scene component and rename it SpawnPoint .

The tunnel mesh has a length of 2500 units on the X axis, so that's where the connection points should be. Go to the Details panel and set the Location property to (2500, 0, 0) .

The next thing to do is create a function that spawns the tunnel at SpawnPoint .

Spawn a new production point in the tunnel

Click Compile , then switch to BP_TunnelSpawner .

The next BP_Tunnel should spawn at the SpawnPoint of the furthest tunnel . By doing this, the tunnel will always continue.

Since the furthest tunnel is always the last one generated, you can easily get a reference to it.

Open the SpawnTunnel 's graph. Right-click on the Return Value pin of the Spawn Actor From Class node . Select Promote to Variable and rename the variable to NewestTunnel .

Now you will always refer to the furthest tunnel.

Next, create a new function and name it SpawnTunnelAtSpawnPoint . Create the following graph:

This setting will get the location of the latest tunnel and its SpawnPoint component. It will then generate a new tunnel at this location.

In order for a BP_Tunnel to communicate with a BP_TunnelSpawner , it needs a reference. If there is no communication, BP_TunnelSpawner will not know when to spawn the next tunnel.

Create a reference to the Tunnel Spawner

Click Compile , then close the SpawnTunnelAtSpawnPoint graph. After that, switch to BP_Tunnel .

Add a new variable and name it TunnelSpawner . Set its variable type to BP_TunnelSpawner\Object Reference.

Click Compile , then switch back to BP_TunnelSpawner .

Open the SpawnTunnel 's graph and add the indicated nodes:

Each tunnel now references a BP_TunnelSpawner .

Next, you'll tell BP_TunnelSpawner to spawn the next tunnel when the player enters the TriggerZone .

Script the trigger zone

Click Compile , then switch to BP_Tunnel .

Go to the Components palette and right click on TriggerZone . Select Add Event\Add OnComponentBeginOverlap . This will add the following nodes to your event graph:

This node will execute whenever another Actor overlaps the TriggerZone.

First, you should check if the Actor overlapping the TriggerZone is a player.

Left click and drag the Other Actor pin. Release left-click on an empty area and select Cast to BP_Player from the menu .

Note: Since the tunnel spawns at the end of another tunnel , it triggers that tunnel's TriggerZone . If the Other Actor is a tunnel, casting to BP_Player will prevent any other nodes from executing.

Next, add the indicated nodes after the Cast to BP_Player node:

Let's go step by step:

  1. When an Actor overlaps the TriggerZone, the On Component Begin Overlap (TriggerZone) node will be executed
  2. Cast to BP_Player node checks if the overlapping Actor is a player
  3. If it is a player, then BP_TunnelSpawner will spawn a new tunnel. Its position will be in the SpawnPoint component of the last spawned tunnel.
  4. Since the old tunnel is no longer used, the game uses a DestroyActor node to remove it

Click Compile , return to the main editor, and press Play . Once you reach the end of the tunnel, the game will spawn a new one.

While the game is endlessly spawning tunnels, it doesn't appear to be endless. You can mitigate this by always showing some tunnels. Later, when you combine this with obstacles, the player will not be able to see the resulting tunnel.

generate more tunnels

The first thing to do is create a function that spawns a certain number of tunnels.

Open BP_TunnelSpawner and create a new function called SpawnInitialTunnels .

To generate a specified number of tunnels you can use a ForLoop node. The node will execute the connected node the specified number of times. Add a ForLoop node and connect it to the Entry node.

To make the ForLoop node execute n times, you need to set the Last Index to n – 1 .

In this tutorial, you will generate three tunnels. To perform three loops, set the Last Index value to 2 .

Note: If you don't set the First Index or Last Index fields, they will default to 0

When the game starts, the player should always start in the tunnel. To do this, you can generate the first tunnel at the player's location.

spawn the first tunnel

To determine if the first tunnel has been generated, you can check if NewestTunnel is set . If not set, it means that the first tunnel has not been generated yet. This is because NewestTunnel is only set after the game has generated the tunnel .

To perform this check, add an IsValid node (the one with the question mark icon) after the ForLoop node.

Next, get a reference to NewestTunnel and connect it to the Input Object pin of the IsValid node .

If NewestTunnel is not set , the Is Not Valid pin will be executed and vice versa.

Add the following and connect it to the Is Not Valid pin of the IsValid node :

This setting will generate a tunnel at the Player Pawn's location.

Next, you'll generate the follow-up tunnel.

Subsequent tunnel

Add a SpawnTunnelAtSpawnPoint node and connect it to the Is Valid pin of the IsValid node.

Here's the final graph:

Summary:

  1. The ForLoop node will be executed 3 times in total
  2. In the first loop, it will spawn a tunnel at the player's position
  3. On subsequent loops, it will spawn a tunnel at the SpawnPoint of the latest tunnel

Next, go to the event graph and delete the SpawnTunnel node. After that , add the SpawnInitialTunnels node after the Event BeginPlay .

Now, when the game starts, it will spawn three tunnels.

Click Compile , return to the main editor, and press Play . Tunnels are now longer!

The game isn't very challenging right now, so let's add some obstacles.

create barriers

Here is the mesh you will use as the obstacle:

Open BP_Tunnel and go to the Components panel. Add a Static Mesh Component and name it WallMesh .

Go to the Details panel and change its Static Mesh property to SM_Hole_01 .

Next, set its Location property to (2470, 0, 0) . This will place it at the end of the tunnel.

To make the game more interesting, the walls also rotate. Add a new Float variable and name it RotateSpeed . Set the default value to 30 .

Switch to the Event Graph and find the Event Tick node. Create the following settings:

This will rotate the WallMesh every frame by the amount provided.

Click Compile and return to the main editor. Press play to see the rotating walls.

Let's spice things up by adding some variety to the walls.

Create Wall Variations

Instead of creating a new blueprint for each variant, you can just randomize the WallMesh .

Open BP_Tunnel and create a new function called RandomizeWall . Afterwards, create the following graph:

As the name suggests, the Set Static Mesh node will set the WallMesh to the provided mesh.

To make a list of static meshes you can use a Select node.

Left click and drag the New Mesh pin. Release left-click on an empty area, and add a Select node.

The Select node allows you to set a list of options. The Index input determines the options for the Select node output.

Since there are four wall meshes available, you need to create two more option pins. You can do this by right-clicking on the Select node and selecting Add Option Pin . Do this until you have four option pins.

Next, set each option to the following:

  • Option 0:  SM_Hole_01
  • Option 1:  SM_Hole_02
  • Option 2:  SM_Hole_03
  • Option 3:  SM_Hole_04

Now, let's choose a random option.

randomization wall

You can use the Random Integer in the Range node to get random numbers. This node will return a value >= Min and <= Max .

Add a Random Integer to the Range node and connect it to the Index pin of the Select node .

Set the maximum value to 3 . This will give you four possible numbers: 0, 1, 2 and 3.

To create more randomization, let's add random rotation to the WallMesh . Add the following after the Set Static Mesh node:

This will add a random rotation between 0 and 360 degrees to the WallMesh.

Here's the final graph:

Summary:

  1. Select node provides grid list
  2. Use the Random Integer in Range node to select a random grid
  3. The Set Static Mesh node sets the WallMesh to the selected mesh
  4. The AddLocalRotation node adds a random rotation offset to the WallMesh

Click Compile and close the RandomizeWall graph.

Switch to BP_TunnelSpawner and open the SpawnTunnel graph. Add highlighted nodes:

Now, whenever a tunnel spawns, it will have a random wall mesh.

Close the SpawnTunnel diagram and click Compile . Go back to the main editor and press play to see all the wall changes!

If you hit a wall, you will stop going. However, if you walk around and go through a hole, you'll start moving forward again.

The next step is to disable forward movement when the player collides with a wall.

Handling Wall Collisions

To enable or disable forward movement you can use a boolean variable. They have only two states: true and false .

Open BP_Player , and create a new boolean variable called IsDead .

Next, go to the Event Tick node and create a Branch node.

Then, get a reference to IsDead and connect it to the Branch node's Condition pin.

Connect the Event Tick node to the Branch node. Then, connect the False pin of the Branch node to the AddActorWorldOffset node.

Now, as long as IsDead is set to true , the player will stop advancing.

Next, let's set the IsDead variable for when the player hits the wall .

Set the IsDead variable

Click Compile , then switch to BP_Tunnel . In the Components panel, right-click on WallMesh and select Add Event\Add OnComponentHit . This will add the following nodes to your event graph:

This node will execute whenever another Actor collides with the WallMesh.

First, you need to check if the Actor colliding with the WallMesh is a player.

Left click and drag the Other Actor pin. Release left-click on an empty area and select Cast to BP_Player from the menu .

Next, left click and drag Cast's BP_Player pin to the BP_Player node. Release left click on an empty area and add a Set Is Dead node.

Set IsDead to true by left clicking on the checkbox .

Click Compile and return to the main editor. Press play and try to hit the wall. If you move into a hole, you will no longer go through it.

In the next section, you'll display a restart button when the player hits a wall.

Show restart button

The widget you will display is named WBP_Restart . You can find it in the UI folder. Here's what it looks like:

To show or hide a widget, you need a reference to it. Open BP_Player , and create a new variable called RestartWidget . Change the variable type to WBP_Restart\Object Reference .

Next, go to the Event Graph and find the Event BeginPlay node.

Add a Create Widget node and set the Class value to WBP_Restart .

After that, add a Set Restart Widget node, and wire everything up like this:

Now, when the player spawns, it will create an instance of WBP_Restart . The next step is to create a function that displays this instance.

Create a display function

Create a new function and name it DisplayRestart . Once done, create the following graph:

Summary:

  1. Add to Viewport will show RestartWidget on screen
  2. Set Input Mode UI Only  will limit the player's interaction with the UI. This way the player can't walk around while they die.
  3. As the name suggests, Set Show Mouse Cursor just shows the mouse cursor

To display the restart button, you simply call DisplayRestart after the player collides with a wall .

Call the display function

Close the DisplayRestart graphic and click Compile .

Switch to BP_Tunnel and find the On Component Hit (WallMesh) node.

Add a DisplayRestart node to the end of the node chain.

Click Compile , then close BP_Tunnel . Go back to the main editor and press Play . If you hit a wall, a restart button will appear.

The final step is to restart the game when the player clicks the button.

restart the game

There are two things that need to be done when the game restarts:

  1. Reset Player. This includes removing the restart button from the screen.
  2. Rebirth Tunnel. This way the player starts at the beginning of the tunnel.

Let's start by resetting Player.

Reset Player

Open BP_Player , and create a new function called RestartGame . Create the following graph:

Summary:

  1. Set Is Dead sets IsDead to false . This re-enables forward motion.
  2. Remove From Parent removes the RestartWidget from the screen
  3. Set Input Mode Game Only re-enables game input so players can move around
  4. Set Show Mouse Cursor hides the mouse cursor

Next, let's respawn the tunnel.

rebirth tunnel

Click Compile , then close BP_Player .

Open BP_TunnelSpawner and make sure you are in the SpawnInitialTunnels graph.

First, you need to remove the existing tunnel before generating the new one.

Add a Sequence node after the Entry node . Connect the Then 1 pin to the ForLoop node.

Note:  The Sequence node executes its output sequentially. This is a great way to organize graphs vertically, especially since node chains can get quite long.

Next, create the following nodes:

This setting will take all existing tunnels and remove them from the game.

Finally, connect the Then 0 pin of the Sequence node to the Get All Actors of Class node. This will ensure that the tunnel is removed before the point process is spawned.

Here's the final graph:

The last thing to do is handle the button click.

Handle button clicks

Click Compile , then close BP_TunnelSpawner .

Go to the Content Browser and navigate to the UI folder. Double click on WBP_Restart to open it.

Select the RestartButton , then go to the Details panel. Go to the Events section and click the button next to OnClicked .

This will create a node called On Clicked (RestartButton) . This node will be executed when the player clicks the RestartButton .

Recreate the following:

Summary:

  1. Get Owning Player Pawn Returns the Pawn currently controlled by the player
  2. Cast to BP_Player checks if the Pawn belongs to the BP_Player class
  3. If yes, it calls the RestartGame function. This function resets the Player and hides the restart button.
  4. Get All Actors of Class and Get returns BP_TunnelSpawner and then calls SpawnInitialTunnels . This function will delete the existing tunnel and generate a new one.
NOTE: You might be wondering why I used Get All Actors Of Class instead of using a reference to BP_TunnelSpawner . The main reason is because BP_Tunnel has no relationship with WBP_Restart. For a simple game like this, it's easier to do the above than to figure out where the reference is stored.

Click Compile and close the blueprint editor. Press the Play Test restart button!

Guess you like

Origin blog.csdn.net/qq_52825422/article/details/126652707