- Home /
AI movement with other factors such as paths that increase walking speed
I've been thinking about finally approaching AI within my projects as it limits me not being able to do much without so (plus knowing AI would make my university life easier within my degree)
Right now I am gonna start off simple with movement.
Context: An NPC has a certain path desired (such as a saved point based on mouseclick on screen). Ideally, the NPC would walk straight over to the point and find the shortest path. Sounds simple. Now, what if instead of walking the shortest distance, there is a path which increases movement when walking on it. Overall, the travel time would be shorter due to the longer path having a higher movement speed.
Question: How would I go about choosing the right path based on shortest travel time? Context should give an example. I know A* would be able to find the shortest path, but I am unsure what my options are if I were to change movement speed on a certain part of my level.
Keep in mind I am NOT asking for code but for how to go about this as I would love to learn this and not just copy/pasta. I know Google might have the answer but I am simply just bad at figuring out what to Google.
Thank you, everyone :)
Answer by Jordash · Oct 27, 2018 at 01:36 AM
I'm gathering you are implementing the path finding yourself from scratch, so I won't talk about the built in path finding. Fundamentally the A* algorithm allows you to weight paths, so an area that is traversed faster by your agents can be weighted in such a way it appears to be shorter than a slower path of equal length.
There are many ways to implement a A* as well as how you define these faster areas for an agent to understand, so I won't get too much into that, but this is a great resource for path finding: Red Blob Games. which should get you started on the right track. Most implementations are valid, it all comes down to the type of game you are making and what is most appropriate.
I will for sure have a look at the post you sent :) I am wondering how different it is in C# as I've noticed the blog uses Python to explain the code
There are some c# snippets here but I don't think it goes all the way to A* like the python code does. The best part of Red Blob is the explanations though, once you understand the concept it should be much easier to develop your own code.
try a simple a* star algo first. i would recommend googling a simple drawn out explaination of how it works(without code) so you can wrap your brain around the concept before you start coding.
try coding it without the added preferences between the nodes first. you can always add the preferences between nodes to your node class afterwords. to set up a* in a grid pattern you can use an array of arrays in c#. probly of your custom node class to hold your info. then you can move through your x and y coordinates like this: myarrayofarrays[x][y] = whatever.
if you are not wanting to use a regular grid you can also use an array containing lists of connections to the other indexes in the array and move through your search pattern that way.
Your answer
![](https://koobas.hobune.stream/wayback/20220612181026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Isometric Movement Path (8 Directions) 2 Answers
Characters contending for same position 1 Answer
pathfinding with instant movement(no turning) 0 Answers
Non-player movement 1 Answer