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

Squad AI

Discussion in 'Scripting' started by armorhide406, Jun 12, 2016.

  1. armorhide406

    armorhide406

    Joined:
    Mar 18, 2016
    Posts:
    16
    So you've probably played a game featuring the subject in the title, but how many of you here would happen to know how to implement that in C# using Unity's API?

    Obviously pathfinding and then commands in this case are very difficult subjects, but does anyone know a quick and dirty way to do it to just build a proof of concept? It's a First Person Shooter with two squadmates and the biggest thing is to get them to follow and attack on your behalf.

    General tips or generic code are greatly appreciated.
    Thank you!
     
    Last edited: Jun 12, 2016
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    You can use unity's built in nav system. It has limitations because its nav mesh is baked so you cant really edit the terrain. this is good for games with a static environment. Your right pathfinding is fairly advanced and its even harder to optimise it so your game runs at a reasonable frame rate. The commands required would be relatively simple compared to pathfinding, basically enums, coroutines and switch statements would be the foundation for a command structure.
     
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    As @Craig8726 suggested, Unity has a built-in navigation system. See NavMesh and NavMeshAgent. This system essentially enables your AIs to be aware of the environment and enable them to navigate from a starting point A to a destination B. So you can use this system to make them follow the player by finding the shortest path and avoid obstacles and such.

    For defining how your AIs cooperate with the player, I suggest to learn about Behaviour Tree. It's a modern AI technique which allows you to describe how the AIs behave in a manageable way.

    Have a look at Panda BT (www.pandabehaviour.com), it's a scripting framework based on Behaviour Tree that I am maintaining. The package contains various examples, including a third person shooter that use NavMesh and implements behaviour such as patrol-chase-attack and cover. However, there is no example of cooperation with the player. Though it would be easy to implement such behaviour. If you are interested by using this package, I could help by providing a cooperation example.