• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by dasbin · Jan 12, 2012 at 07:41 AM · aipathfindingforcefollownpc

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image suyujin · Jan 12, 2012 at 07:48 AM 0
Share

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.

avatar image dasbin · Jan 12, 2012 at 07:57 AM 0
Share

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.

avatar image suyujin · Jan 15, 2012 at 09:40 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use A* pathfinding for a large map? 2 Answers

Make NPC follow player when seen? 2 Answers

A* or AStar Pathfinding doesnt work for me? 0 Answers

NPC code, works great, besides them being in the ground. 2 Answers

ai pathfinding project getting started scene problems 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges