Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SpaceShip AI navigation [Discussion] [Answered]

Discussion in 'Scripting' started by GNGification, Oct 31, 2015.

  1. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    TL;DR at the bottom.

    First, sorry if this isn't the best place for this as I do not actually have any code related problems but rather want to find the best way to do something. Also the navigation forum part seems to be more related to navmesh so I wasn't sure where to post this. So if this is in a wrong section, I'm sorry about that.

    I've been wanting to build spaceship AI for a while and I have been looking into different concepts and trying to figure out which way I should go. However I've been unable to decide what I want to do. The only thing that is truly a problem is proper pathfinding as everything else is rather simple to create.

    This scene would be based in a rather empty space area, however the biggest problem is that sometimes the AI would need to dodge moving objects and perhaps even need to navigate through more complex areas. Also this script would include a state machine for some basic stuff like: Seeking, Engaging, Tracking, Fleeing, Evading and stuff like that.

    Now for a solution I've thought about 3 different thing.

    1. Create 3D nodes and use A* for pathfinding. To be honest I'm not even entirely sure if this is possible/recommended as I'm unable to find any proper videos or tutorials when it comes to 3D nodes, all I can find is 2D. Doing it layer style could also work but with or without it, doing pathfinding like this sounds EXPENSIVE. Especially considering that it needs to be updated for moving objects.

    2. Waypoints and ray casting to avoid objects. This one is pretty simple and easy to make but it doesn't seem like the best possible solution. There are plenty of tutorials for this one and AI in general with waypoints but I want to consider other options before using this.

    3. Also there is something called "Steering Behaviors for Autonomous Characters". I did some reading about this but it seems rather complex and I'm not even sure if this is what I'm looking for. So if someone understands this concept well I would be very happy if you could explain it to me or just simply say whether I should use it or not. I'm actually going to take a look into it right now so... yea.

    If you have any suggestions for any methods or general tips, I'd be glad to hear about them. Also if you know a really good tutorial or methods for creating realistic spaceship physics I'm also interested into them. If you want to discuss more about this you can add me on skype (PM for that).

    Just to make sure this doesn't become a big discussion thread I will accept any response as an answer that actually helps me to decide/find a good way of doing this. Also I'm not looking for ready scripts, however examples are nice.

    TL;DR
    I'm looking for a good way to handle spaceship navigation in a rather empty (but sometimes possibly complex) area.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    My typical approach to an AI like this is to use multiple layers.

    Use A* to do pathfinding around all of your static objects and slow moving objects. A* is dimension agnostic. All it cares about is that you can create a network of nodes with links to other nodes.

    Your pathfinding layer feeds each waypoint into your steering layer. Steering is pretty straight forward once you get the concepts. It basically takes a bunch of inputs and converts them into a desired direction. Inputs like direction of next path point, distance and direction of nearby obstacles and so forth.

    Finally you have your motor layer, that actually moves the ship in the desired direction.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    the 1st is the best option with A*. I'm not sure what your interpretation is for the node? they are not waypoint or gameobject in anyway. they are merely a class that stores the position in the grid and the F,G,H values of the A* system.
    no problem doing this 3D as you can just use a multidimensional array of nodes (Node[,,] theGrid)
    the moving object are also no problem they just set the node at there position to occupied orso...
    anyway here is a vid on A*:

    you can find a free A* asset easily...

    as for option 3 this sounds like some Q-learning using an artificial neural net and a genetic algorithm... I' don't know the level of experience you have, but these are the more advanced and more interesting/experimental methods you can use.
    The basic concept is that it learns to approximate a function through trail and error and of course rewards if it does something right.

    good luck.
     
  4. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    This was really helpful, it still seems complicated but I'll figure something out. Also doing pathfinding around objects gave me an amazing idea. Thank you!

    Thanks for clearing that up, I think I'm going to stick with something easier but it certainly sounds interesting.

    I'm pretty sure these answers have given me a good idea how to do this. Thank you for your answers I'll mark this as answered.
     
  5. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    It really depends how complicated the scenes get. If it's one or two objects that can easily be checked using sphere casts/colliders, then it's not even necessary to have explicit pathfinding. Steering behaviors can pretty easily be made to check the proximity of a couple of spheres pretty cheaply. This could even be simple responses to passive physics colliders that are set up on the important objects.

    If things get complicated though, pathfinding can take some atypical turns. In theory and with a lot of set up, it's possible to take the steering method and scale it up. A cluster of asteroids could be grouped together, with a bounding sphere enclosing the group, and then trade ships (or other larger ships) could be set up to always try to skirt around what could be dangerous territory. Fighters wouldn't care and might only worry about immediate dangers.

    The problem with steering based pathing is it's expensive because it's effectively being done continuously. Building an efficient graph for pathing is a major hurdle for A*, otherwise it becomes horribly expensive to run any pathfinding. As much as it sounds bizarre, one of the best ideas is to create "roads" of established paths so agents can take a path without running a full search.
     
    Kiwasi and GNGification like this.
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you wouldn't have to constantly check your whole world grid. you could make a smaller local one around each ship that updates itself by the bigger one for immediate actions like evasion... but yes if it's an average scene you might as well do it with colliders and Physics.OverlapSphere.
     
    GNGification likes this.
  7. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Yes that's the idea I got from BoredMormon, I'm going to consider OverlapSphere too. Thanks for all the responses.