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

3D Space Combat Game AI

Discussion in 'Scripting' started by Kith, Aug 2, 2011.

  1. Kith

    Kith

    Joined:
    Jun 12, 2011
    Posts:
    94
    Hello everyone,
    We just finished our first project Trace Invaders, and we're looking to start a new project which is a space combat game complete with enemy AI's. The biggest problem I've run across was accounting for obstacles in between the enemy and the player. I was trying to find some resources on how to code something like that. I've come across a lot of references to A* and Dijkstra's, but I don't understand how that can apply to 3-Dimensional space with no nodes/tiles. Does anyone have any useful links or resources they can point me towards?

    Thanks!
     
  2. RetepTrun

    RetepTrun

    Joined:
    Jul 31, 2011
    Posts:
    9
    I am making a space game too.

    None of my AI are using physics yet but they do get around without hitting things using raycast. I used this video
    http://vimeo.com/9304844
     
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I've had the same problem!

    Anyways my solution was to come up with my own:

    http://forum.unity3d.com/threads/92021-Simple-But-Effective-AI-Script-Explained


    Or else this site (all about video game AI) can maybe help you:

    http://aigamedev.com/

    Or else come up with your own (what I would recommend) - the best way to do this is:

    1. Start off your AI script with just a simple "look at and shoot always"
    2. Then go for "only look at and shoot if he's alerted"
    3. When is he alerted? i.e. when the player walks in front of him or near him?
    4. Now what happens if don't exactly come near him, but close enough that he hears a noise?
    5. What if the player starts shooting back? He has to find cover! Move over to the closest waypoint of a specific set of waypoints (which would be behind walls)
    6. Now the player's not moving - he's staying in cover! What do you do? Maybe approach him? Or call your teammates?
    7. Finally, what if there are other teammates around you? They would want to know when you are alerted! (send a signal to all objects maybe)

    Doing it step - by - step is a lot easier then copying and pasting someone else's code. (or maybe you just need to modify that code :p )
     
  4. Kith

    Kith

    Joined:
    Jun 12, 2011
    Posts:
    94
    I suppose my real question then is, is it still viable to use node based pathfinding in a situation with complete 3-dimensional mobility. Think Starfox 64 (All range mode).
     
  5. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Node based pathfinding?

    Could you explain a little more?

    Maybe what you're trying to go for is waypoints? (You could use a series of waypoints to navigate an npc around the world)
     
  6. Kith

    Kith

    Joined:
    Jun 12, 2011
    Posts:
    94
    Many common pathfinding algorithms like A* use weighted nodes to determine possible paths to an objective. This works very well in 2D space, but I was wondering how it is done in 3D? Or if it is even done in 3D like that? I'm just curious as to what the common consensus is about it. I'm going to be buying a book soon about the topic, but I really want to start learning ASAP.
     
  7. niosop2

    niosop2

    Joined:
    Jul 23, 2009
    Posts:
    1,059
    A* is a generic solver. It's used a lot for 2D/grid based stuff, so most of the implementations are geared towards that, but it could be used for anything from 3D space or even generic problem solving. Check out Micropather (http://www.grinninglizard.com/MicroPather/). It's implementation is pretty generic.

    Pathfinding is probably not used very often for space games though, except maybe for finding paths between warp gates or something. Just because there's a lot of chunks when dealing with 3D, so you have to store a bunch more path finding data. Best bet would likely be dealing with it using raycasting and steering.
     
    Last edited: Aug 2, 2011
  8. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Well there you have it - I have no clue how to use A* anyway.
     
  9. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Consider using something similar to OpenSteer. Here it's called UnitySteer. It is a set of tools for Unity that were developed to do a few different behaviors similar to what you want. Obstacle avoidance, chasing fleeing and such, all in there.
     
  10. tectuma

    tectuma

    Joined:
    Nov 26, 2012
    Posts:
    46
    Glad I am not the only one fighting with this... I the post on the subject years ago but it has all ways been a thorn in my side. I am getting to the point in my project that I am going to work the issue once more and see what I can come up with. I tried to use unitysteer but never could get it to work right with the scale of my space ships... I wish someone had a package like Rain for space ships that did not depend on nav meshes... :/

    Edit Found my old post from May 2, 2014: http://forum.unity3d.com/threads/simple-space-ship-object-avoidance-help-needed.243937/

    Well any way if you want swing by http://www.twitch.tv/tectuma and give me a shout... maybe if some of us put our heads together we can figure it out... lol
     
    Last edited: Sep 24, 2015
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    3D pathfinding is really no different to 2D. You just have more space to work with. The general principles of A* will still work. As do the general principles of steering behaviours.

    You can do dynamic 3D path finding by
    • Setting up a series of nodes around each collider
    • Get a list of nodes
    • Using raycasting to determine passability between each node
    • Run the resulting network through an A* algorithm to get the path
    I have no current need for 3D path finding. But I might build something one day for kicks and giggles.
     
    Paykoman likes this.
  12. tectuma

    tectuma

    Joined:
    Nov 26, 2012
    Posts:
    46
    I have played with Rain but not A*. Last night I worked on getting the Alien ships collecting and being able to destroy them. To night I plan on working on getting my robot ships up to par with the Alien ships with the new scrip and if I have time teach them to hate each other. At this time the ships have no object avoidance at all.

    What makes 3D harder is with 2D you have 2 choices Left and Right, and most objects or stationary (like walls). In space now you can go any direction to get around and also you now have 2 objects that are moving trying to avoid each other.

    I personally am thinking about doing racast or using a trigger sphere around the ships. I all ready plan on dropping Rain in for the NPC (non ships). Dropping A* may not be a good idea considering the project is all ready huge... lol
     
  13. fermas

    fermas

    Joined:
    Sep 21, 2015
    Posts:
    15
    For A* applied to 3D settings, search for cubic nodes, or cubic tiles. I've already read papers on that. It is not that uncommon. However, I don't think it is practical for a space game. The reason being that space has a lot of space. Which means, the number of tridimensional tiles increase too fast, making the array that store tiles to become too big memory-wise and therefore the A* too slow performance-wise.

    I think what you need is steer behavior techniques, i.e. approaches that only are turned on to maneuver around obstacles when obstacles are close enough. For a very advanced technique, search for RVO2.
     
    Kiwasi likes this.