Search Unity

Pathfinding/AI Implementation - Can I Get A Sanity Check?

Discussion in 'Navigation' started by spelley, Jan 28, 2018.

  1. spelley

    spelley

    Joined:
    Jul 11, 2012
    Posts:
    2
    Hey everyone,

    I'm quite new to Unity (~3 months or so) and game development in general and have undertaken working on a turn-based tactical RPG. Using that as an excuse to touch a ton of different systems.

    The game basically has a similar setup as Divinity: Original Sin with free-roaming exploration, enemies on map and when you are seen/engage, battle goes into turn-based, and movement and skills are based on an Action Point-like system.

    Currently, I'm working on the combination Pathfinding/AI system after engagement has already started. The method I'm using right now is as follows:

    • Node Generation (Pre-Fight): Generates a grid of nodes by Raycasting from above every X units (currently 2 - 2.5 units seems good for performance and being granular enough) on the X and Y axis. These designate places the unit check for the purposes of movement.

    • Skill Profile: Unit uses a combination of boolean flags that determines priorities of skills. It then prioritizes and picks the top 5-10 skills it has available (assuming there are more than that, which there usually isn't) and notes their qualities. Largest AOE, smallest AOE, whether any skill cares about Line of Sight, etc.

    • GridTargets: Get list of targets in the fight, both friendly and enemy. Notes their positions. Each grid target also knows the location of every other GridTarget within the largest AOE range based on the Skill Profile.

    • Mid-Points: Create up to 2 extra GridTargets per unit by creating a Bounding box around each unit that encapsulates all other units within the AOE range of the biggest/smallest AOE range from the Skill Profile. Creates a GridTarget at the center of that bounding box. This midpoint also knows the distances to each other unit.

    • Reachable Nodes: Get nodes that are reachable based on stats from current position. Do Line of Sight checks using raycasts if Skill Profile says we need it. Use agent.CalculatePath to determine if the spot is reachable, and calculate how long the path of points is to make it's actually in range.

    • Skill Check: Now, given all of the above we iterate through each reachable node with each GridTarget to create a NodeScenario. While we add Node Scenarios, we have a background thread processing through them and running them against each skill.
    Each skill has a "Skill Check" function that returns a float which basically gives it a score. Most checks result in a "0" as they are eliminated due to lack of Line of Sight, being in the blast range of an AOE, casting a Heal on an enemy/casting a damaging skill on an ally, requiring too many Action Points, etc. The top 5 scores are kept. A weighted, randomized one of those is then chosen.

    The above happens within approximately 1 - 1.5s in some worst case scenarios. Most of this time can be covered up from the animations between switching characters, moving the UI into place, etc.

    What I mostly am curious about is if I am missing something that would either greatly speed this up, a far saner approach to doing all this, or just general feedback on what I could be doing better. As I said, I'm not a game developer by trade so any tips would be greatly appreciated.
     
  2. Miggi124

    Miggi124

    Joined:
    Jul 31, 2017
    Posts:
    69
    Hi,

    Miggi124 here. I admire your effort, but mate - don't overcomplicate things. I recommend looking into Unity's build in pathfinding system - it's pretty decent and will save you a hell of a lot of time. It probably is more efficient, optimized and able to do what you need it to do in your game. If you have any specific questions about pathfinding, I'm pretty decent with A* and advanced pathfinding, but I'm sure this will help for now. Good luck with your game.


    -Miggi124