• 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
0
Question by SuperRaed · Jun 30, 2016 at 07:41 AM · aipathfindingpathalgorithmartificial intelligence

how do I add abilities(Scripts/GameObjects) to my path finding agent

I want to make an autonomous moving agent that in it's most raw form simply just moves forward, but I want to be able to attach scripts to it, preferably during runtime, that gives it abilities for example A* pathfinding algorimthm...etc my very first though was to publicly define a script variable that on Start() checks if it's null or not and change a bool value accordingly, which during run time and using an if statement will cause the agent to behave differently. while this may work is it a good way though, can I do it differently?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JoshuaMcKenzie · Jun 30, 2016 at 10:02 AM

So you want to dynamically alter the behavior (of how a gameobject moves) of a class at run-time. there's a couple of solid design patterns for this.

The Strategy pattern is a simple pattern to implement. you have a class that publicly stores a reference to sub classes that it will run during an event. for example a Monobehaviour script has a public variable to an IMoveable interface with a function Move() that it will run in Update(). IMoveable could be a DefaultForwardMovement Class or a PathfindingMovement class that can be swapped out at runtime. The pattern is pretty simple and straight forward to implement. and should be a pretty simple concept to grasp

The State Machine Pattern is nearly identical to the Strategy pattern, except that the IMoveable references aren't publicly available, but internally managed. This is ideal if you want the Gameobject to strongly control when and how it will change its movement behaviour.

The Decorator design pattern is great if you want to partially override a object behaviour over different lifecycles (say that a ship is now using a pathfinding agent due for some special event, but keep using it's default movespeed, while a 2nd effect jammed the rudder and it can't turn). its also great for stacking overridden behaviors together. Decorators are a lot more flexible, but also much more complicated

first off make a mediator class, a MovementBehaviour class that derives from Monobehaviour. whose sole purpose is to allow other monobehaviour classes indirect access to a stable reference (so they don't need to worry about broken references when a decorator is swapped).When an outside class tries to call Move() on the MovementBehaviour class, the class will call the Move() function for the current decorator that's active (either to move straight forward, or to follow a Navagent).

with the decorator pattern you write a concrete class that will perform some default action when a class calls that action to be done. but then you can have a decorator that can wrap around the concrete class and pretend to be that class that will run the actions (but will be doing something different. this decorator class will keep a reference to the class its wrapping for when some value thats not specific to the decorator is needed (such as a move speed for a pathfinding decorator) by grabbing it from the class its wrapping. and decorators can wrap other decorators stacking effects together

Any of these options are usable, just that some will be better fit for your game than others, depending on what your specific requirements are. if you believe that you'll only have those two movement modes, then just any straightforward technique should do (tho I recommend against it)

Comment
Add comment · Show 1 · Share
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 SuperRaed · Jul 01, 2016 at 12:48 PM 0
Share

great answer, for my project I decided to go with State machines and it's exaclty what I was looking for

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.

Follow this Question

Answers Answers and Comments

73 People are following this question.

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

Related Questions

Start/End zigzag coverage region algorithm 1 Answer

If Terrain is larger(width10000Xlength10000) then how to increase the size of the GridGraph of Astar Path Finder 0 Answers

AI teleporting 1 Answer

A* optimization and path help 0 Answers

Is there any way to make the vertical automatically-generated Mesh Links from the NavMesh bidirectional? 0 Answers


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