- Home /
Sidescroller NPC follows Player
This is for a 2D sidescroller.
I'm trying to figure out the best logistics of doing this. I'm not looking for anyone to write scripts for me, merely brainstorming some logical ideas I can start building scripts from. I'm fairly new to Unity.
Basically, the player can hold down a button that will put nearby NPC's into a "following player" mode. No problems so far.
I want the NPC's (all rigidbodies, as is the player character) to follow in a line behind the player (there will be several NPC's following one after another), over complex terrain and over jumps. Also want to insert some randomness to their follow distance over time, though they should stay within certain bounds. Because I want them to stay grounded over rough terrain, and they are rigidbodies with gravity, it makes the most sense to be applying forces to get to the player. But it seems to me that the forces should not necessarily actually be pointing in the exact direction of the player - because, for example, if the player is somewhat above the NPC, the NPC will start floating. Perhaps a force in the X-axis only would work?
The player script maintains constant speed over all terrain, despite being controlled using forces as well.
I have no idea where to start for getting them to jump. Obviously don't want them to jump at the exact moment the player pushes jump, but rather when they reach the spot the player jumped at, so they can make it to the next platform. How would you approach doing this?
Same question, but perhaps requiring a different solution, for climbing ladders. For the player this is handled not with movement axis, but basically by engaging a separate up and down Transform on the player and disengaging gravity while on the ladder.
Additionally, there is an "air move" component to the player control. The player has some limited movement control while in the air, a la Mario, and the NPC's will need to follow this movement as well.
No idea how to handle environmental disturbances, either - if some other object manages to get between the player and the NPC, then what would you do? Just cause the follow to break? But how to determine when to break, and when to just increase speed to try to catch up? It seems far too complicated to try to get the NPC to try to navigate over/around the disturbance, etc.
Additionally, are there any Assets available that could help me out here? Would AI pathfinding be of use, even though the NPC's should be directly behind the player at all times?
Any other thoughts or concerns you might have about implementing this sort of thing would be welcomed.
Thanks, and sorry for the broad scope of the question... I'm a little stuck.
If you have your npcs capable of movement already, you can just set a delay to their movement controls so they mimic what you do at times behind you, and stop far enough back as to not walk into you. If you don't have them capable of AI movement already, you'll want to approach getting them controls like what the player character has.
If you're able to basically "copy / paste" the movement controls to the npcs, you could set the delay to movement, paired with a little path finding to get them the few feet up to where you started from, then whatever other AI you need for say combat reactions or things of that nature.
Hope this helps a little.
Thanks! Never thought of just delaying them, although it seems the time necessary would be dependent on the current velocity.
I should add a bit of detail here: the NPC's are not really NPC's but actually characters that the player can switch between controlling. They otherwise can't move: either the player is controlling one directly, or they are standing still, or they are following the player, nothing else.
Well if you want it to look the best, you should approach it with a path finding system so they'll follow you and overcome obstacles as they see them (jumping over things, climbing, etc.) ins$$anonymous$$d of mimicking what you do with a delay. That will take a bit longer to code, though.
Your answer
Follow this Question
Related Questions
How i make the NPC friend stop following me and start to follow the enemy when he is near???!!! 0 Answers
How to use A* pathfinding for a large map? 2 Answers
A* or AStar Pathfinding doesnt work for me? 0 Answers
Make NPC follow player when seen? 2 Answers
NPC code, works great, besides them being in the ground. 2 Answers