“Hey, you! Let’s battle!” Adding a Line of Sight mechanic to your NPCs

13

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

npc-los-example.c3p

Download now 255.98 KB

Stats

5,204 visits, 8,855 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 15 Oct, 2019.

Adding the mechanic to other NPCs

It wouldn’t be much of a game if only the un-moving NPCs had this functionality attached to them, so let’s add it to the other variations!

Rotating NPCs

When just dealing with the talking interaction, we didn’t need to really worry about the direction on the rotating NPCs. However, this challenge mechanic relies on the direction to cast the ray, so we need to make sure these NPCs are assigning themselves a direction. When the NPC is in any of the ‘rotating’ states (that’s Clockwise and AntiClockwise for our non-random and Rotating for our random NPCs) then we need to assign a direction based on the animation frame currently playing:

Condition

Rotate_NPC_Sprite ▶︎ State = “Clockwise”

Sub-event Condition

Rotate_NPC_Sprite ▶︎ Animation frame = 0

Sub-event Action

Rotate_NPC_Sprite ▶︎ Set Direction to “Down”

Repeat this sub-event for the remaining three directions, and then for the AntiClockwise and Rotating states. Remember that for the AntiClockwise state, your left-facing and right-facing animation frames will be reversed compared to Clockwise.

That’s the only extra functionality you need to add for the two rotating types of NPCs, now you can add the rest of the changes that you made to the Static NPC.

Path-Based NPCs

Adding this mechanic to the Set Path NPC is the same as you did for the Static NPC, just make sure you’re using InterruptedState and InterruptedDirection as opposed to InterruptedState2 etc.

For the Random Path NPC however, we’ll need to take into account the object’s Timers so that it doesn’t carry on moving when we’ve triggered the LOS actions. All this takes, however, is one extra action in the Ray intersected event block.

In the sub-event where we check if the NPC_Base RayHitID equals the Player’s UID, you simply need to add the following action to the top of the list of actions that trigger:

RandomPath_NPC_Sprite ▶︎ Stop Timer “RandomNPCMove”

This will stop the timer so that the NPC never picks a new state or direction and means that the state changes associated with the ‘battle’ mechanic can continue as they should. Then, as the NPC will have the StartFight state when the layout is started again following the BattleScene, they’ll have the state and direction they started with, which should trigger the timer to start again.

And that’s us done! Now you should have a functioning system where your NPCs can notice the player and trigger an action. There’s probably a lot of tidying that could be done with this project, but I’ve kept a lot of things separate to try and better illustrate what’s actually happening in the project.

Hope you found this useful!

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!