Hey Chris,
I want to make an NPC (a monster) appear and start chasing the player whenever the player runs in the game. The NPC should spawn every time the player runs.
Here’s how I’ve implemented it so far:
I created a 2D Trigger set to Continuous and Run in Background, covering the entire scene.
I added two 2D Markers—one for the NPC’s spawn point and the other on the opposite side of the scene.
In the Trigger’s ActionList, I used Input: Check with the input set to "Run", followed by a Character: Move to Point action to make the NPC move.
The system works to some extent, but it’s not perfect. I’d really appreciate your help with the following issues:
1) I’m currently using Input: Check to detect the Run input. The issue is that the NPC gets triggered even if the player is just standing still and presses the Run button. What’s the best and simplest way to detect whether the player is actually running—not just pressing the input?
2) Every time I press the Run input while the NPC is already in the scene, it disappears and respawns at the Marker. How can I prevent the NPC from respawning if it’s already active?
3) I want the NPC to spawn randomly from either the left or the right side of the scene. What’s the best way to implement random spawning between two predefined 2D Markers?
4) Is it possible to make the NPC continue chasing the player across scenes? If so, how can I implement this in AC?
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
1/ The better way to detect input is to use Active Inputs - which allow you to run ActionLists when a specific input is detected. However in this case, as you say, it's not so much the input you want to check as it is the Player's movement. For this it's probably best done with a script:
2/ Use a Bool variable that's set to True when the monster is active, and check for this before spawning them.
3/ The Variable: Check random number Action can be used to output two sockets that will be chosen at random each time it's run. Use this to run two separate Object: Teleport Actions that move the monster to different ends of the scene.
4/ Convert the NPC into a Player via the cog menu at the top of their Inspector, and then assign them as an additional Player in the Settings Manager (after setting Player switching to Allow). When inactive, AC will treat them like an NPC but will also handle their scene presence automatically. Instead of spawning their prefab with Object: Add or remove, you can instead use the Player: Teleport inactive Action to have them appear in the current scene. If you then update the Character: NPC follow Action to reference the monster as an inactive Player, an additional Follow across scenes? option will be shown.
More on AC's Player-switching feature can be found here.
Thank you for the detailed explanation. 2) and 3) worked perfectly, and I plan to handle 4) later.
However, I'm still having trouble with 1). The NPC gets triggered the moment the scene starts.
For my implementation, I created a GameObject in the scene, attached the script to it, and then linked the Trigger2D ActionList to the script.
https://prnt.sc/SGjFtS3Oq2TX
Any idea what might be the issue?
I've encountered another issue. When I leave the scene while the NPC is chasing me and re-enter the scene quickly, the NPC is still there and immediately starts running toward me again.
I tried adding an Object: Teleport action to the scene exit hotspot to reset the NPC, but it didn’t solve the issue. The NPC does respawn, but it still runs toward the player upon re-entering the scene—even if the player hasn’t run.
How can I properly reset the NPC’s behavior when exiting the scene?
Have it run a Cutscene instead - the Trigger might still be self-starting;
This might be related to the above, if the NPC is being triggered as soon as the scene starts.
It worked perfectly after I switched to a Cutscene. The NPC was still waiting for me when I left the scene, but I managed to solve it by adding Object: Teleport and Character: Move to Point actions to the scene exit hotspots. Now, every time I leave the scene, the NPC is teleported back to the spawn point.
I'm having an issue with Variable: Check random number. When I run while the NPC is moving to a point, the NPC changes direction unexpectedly. Each time I run, the Variable: Check random number action seems to trigger again while the first action is still active. This causes the NPC to get confused and reverse direction mid-movement.
How can I make sure the NPC won't change action while moving to point? I've attached a screenshot of the full ActionList for reference.
https://prnt.sc/kh8xHhx1WrMr
You're putting your Variable: Check Actions after your Variable: Set Actions that affect the same value. Remove the duplicates, and move them to the first and second Actions in the list respectively.
Sorry, Chris—I'm a bit confused here. Is this setup correct? I’m still facing the same issue, so if it’s not, could you please elaborate a bit more? Variables tend to confuse me.
https://prnt.sc/JtwzkoiWV5A8
The underlying intent here is to prevent the Action logic from running if Yaml_Is_Running is True. You can do this with a Variable: Check Action, as you are, and having the ActionList end if it's False.
However, you're running this immediately after your Variable: Set Action - which means it'll always be True when the check is made.
You're also running two branches that both begin with the same two Actions.
What I'm suggesting is to make your first Action the Variable: Check, the second, Variable: Set, and the third, Variable: Check random number.
Thank you for the explanation, Chris. When I set it up like this https://prnt.sc/wvJA6_EETTf0 nothing works—the NPC doesn’t move at all. However, when I change the global variable "Yami_Is_Active" to True instead of False, it works, but the same issue occurs again.
P.S. when removing "Variable: Check random number" the NPC works fine and won't be interrupted when I run again.
Your first Action checks if the Variable is True. Change this to check if it's False to have it only run once. If the False condition is met, the next Action sets it to True and avoids it repeating.
Yes, I tried that yesterday. However, the NPC is only triggered once—after that, it doesn’t move anymore. I suspect the issue might be with the "Variable: Check random number" action. Without it, everything works fine. What do you think?
I’ve tried all possible scenarios, but still no luck—the issue persists.
https://prnt.sc/OVlDlnVmhYuf
Sorry I shared the wrong screenshot. Here's the correct one: https://prnt.sc/SXajrT1_dupn
Is that to say if you just run one of the branches, it works?
The NPC will only get triggered once because of the Variable - you'd need to set it to False again for it to run again, e.g. as the Player exits the scene. I was under the impression that this was due to the Trigger causing it to run multiple times otherwise.
Be aware that you can enable comments in your Actions via their cog menus, and then display these in the Console (from the bottom of the Settings Manager) when they get run. This can help determine which Actions are/aren't being run.
Setting the variable to false when the player exits the scene worked perfectly. Thank you for your help, Chris—I really appreciate it!