• 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 MrCalavera · Jan 18, 2014 at 08:56 PM · c#2dpathfindingdijkstra

Specific 2D movement using Dijkstras Algorithm...

Hi,

I'm playing around with a top down 2D game and i've reached a point where I need to implement some kind of pathfinding. I followed a great tutorial for Dijkstra's Algorithm and i've got my character moving, but not exactly the way I'd like.

Here's my characters movement code: PirateAI.

The way it works is w$$anonymous$$le most of the nodes on my s$$anonymous$$p are empty game objects, some of them are clickable, visible objects. Clicking on one of the visible nodes assigns its gameObject to the target variable in the above script and the selected character moves towards it. T$$anonymous$$s is working. However three t$$anonymous$$ngs are happening that i'd like to change/stop.

First of all the character is able to move diagonally. Is there a way to discourage diagonal movement so the character only moves vertically and horizontally?

Second, the character stops a short distance from the target node instead of directly on top of it. Is there a way to make sure my character stops at the nodes exact position?

Finally when the character stops moving I'm getting the following error over and over: "InvalidOperationException: Operation is not valid due to the current state of the object" Its saying its happening on line 7 of the above linked code.

Anyone have any ideas?

The other scripts used are here: DijkstrasAlgort$$anonymous$$m, CurrentNode, Node

Comment
Add comment · Show 2
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 robertbu · Jan 18, 2014 at 09:18 PM 0
Share
avatar image Owen-Reynolds · Jan 19, 2014 at 12:24 AM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 19, 2014 at 01:46 AM

Pirate is red, target is green. How do you want the pirate to walk? And what is your tilesize?

Walk Styles

  • For blue, check if x-Distance or y-Distance is shorter and walk that first and then the other axis. If you use a Coroutine, you don't need to keep track of what you decided between frames.

  • For yellow alternate the axes if there is still some distance left on the axis.

  • For red, walk the longer axis till both axis are the same, then same as yellow

Let me know if you need more help

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Jan 19, 2014 at 05:15 PM 0
Share
avatar image HappyMoo · Jan 19, 2014 at 05:26 PM 0
Share

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

21 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

Related Questions

A* pathfinding for 2D top Down 0 Answers

Multiple Cars not working 1 Answer

Indexing tiles for pathfinding 1 Answer

Distribute terrain in zones 3 Answers

How do I implement A* pathfinding to my 2d game, without tiles? 4 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