Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 while most of the nodes on my ship 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. This is working. However three things 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: DijkstrasAlgorthim, 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

Just looking at the code fragment you reference, I can suggest a couple of things to look into. Could InvalidOperationException and the stopping short of the goal be due to you calling Pop() on an empty stack?

As for diagonal movement, you may be able to fix the problem by only allowing 'y' movement if 'x' movement is done:

 if ($$anonymous$$athf.Approximatel(transform.position.x, goalPosition.x) {
 
 // all the 'y' translation code goes here
 
 }
avatar image Owen-Reynolds · Jan 19, 2014 at 12:24 AM 0
Share

Not looking, but Dykstra's (and any graph distance algorithm) uses a weight function. Somewhere in the code it's probably checking how far new squares are. Toss in something like "if x and y are both different, distance=99999" and it won't try to go diagonal.

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

Yellow and red are flipped (red is alternate axis)

But this won't search for a path (which is what Djikstra's does, or A*.) It there was a long wall between green and red, this wouldn't know to walk around it, and which way was shorter.

avatar image HappyMoo · Jan 19, 2014 at 05:26 PM 0
Share

He already has waypoints from Djikstra's. He just doesn't want his pirate to walk diagonally, which can be solved like above. If he additionally runs into obstacles depends on how far these waypoints are from each other and how his map looks.

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

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

How to use 2D pathfinding with vector 2 0 Answers

2D Pathfinding: how to create navMesh 0 Answers

Understanding Dijkstra Application 2 Answers

How to compare several distances? 1 Answer

Multiple Cars not working 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