Improving the Blueprint

When we run the game, we see that the Cylinder target changes colors upon being hit by a projectile fired from the player's gun. This is the beginning of a framework of gameplay that can be used to get enemies to respond to the player's Actions. However, you might also have noticed that the target Cylinder changes color, even when the player runs into it directly. Remember that we wanted the Cylinder target to turn red only when hit by a player projectile, and not due to any other object colliding with it. Unforeseen results like this are common whenever scripting is involved, and the best way to avoid them is to check your work by playing the game as often as possible as you are constructing it.

To fix our Blueprint so that the Cylinder target only changes color in response to a player projectile, return to the CylinderTarget_Blueprint tab and look at the Event Hit node again.

The remaining output pins on the Event Hit node are variables that store data about the Event that can be passed to other nodes. The color of the pins represents the kind of data variable they pass. Blue pins pass objects, such as Actors, whereas red pins contain a Boolean (true or false) variable.

The blue output pin, labeled Other, contains a reference to the other Actor that hit the Cylinder target. This is useful for us to ensure that the Cylinder target changes color only when hit by a projectile fired from the player, rather than changing color because of any other Actors that might bump into it.

To ensure that we are only triggering the Cylinder target in response to a player projectile hit, click and drag a wire from the Other output pin to an empty space. In this search window, type projectile. You should see some results that look similar to the following screenshot. The node we are looking for is called Cast To FirstPersonProjectile:

FirstPersonProjectile is a Blueprint included in Unreal Engine 4's First Person template, which controls the behavior of the projectiles that are fired from your gun. This node uses casting to ensure that the Action attached to the execution pin of this node occurs only if the Actor hitting the Cylinder target matches the object referenced by the casting node.

When the node appears, you should already see a blue wire between the Other output pin of the Event Hit node and the Object pin of the casting node. If not, you can generate it manually by clicking and dragging from one pin to the other. You should also remove the connections between the Event Hit and Set Material node execution pins so that the casting node can be linked between them. Removing a wire between two pins can be done by holding down the Alt key and clicking on a pin.

Once you have linked the three nodes, Event Graph should look like this:

Now compile, save, and click on the Play button to test the game again. This time, you should notice that the Cylinder target retains its default color when you walk up and touch it, but if you move away and shoot it, it turns red.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset