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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[RELEASED] Enemy AI

Discussion in 'Assets and Asset Store' started by Viniterra, Mar 24, 2020.

  1. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Hello guys,

    Today I am pleased to announce a new package, for everyone who wants to create any kind of shooter style games: the Enemy AI.

    upload_2020-3-23_21-24-46.png

    Get it now on the Asset Store: Enemy AI

    This package features ready to use enemies that can patrol, search, and engage, looking for cover protection, advancing in player direction, and also can alert other enemies of possible threats.

    This is the standalone package version, for those who are creating, for example, a FPS, or a top down shooter, or any other shooter game. The demo scenes that comes with the package provides some basic player controllers, focusing on the demonstration of the enemy NPCs.

    At any time, if you have acquired the Enemy AI package, and are looking for a professional shooter controller, you can upgrade to the Third Person Shooter Bundle, obtaining the Cover + Shooting System with a discount, comparing to the standalone prices for the packages.

    Tutorial: How to setup the enemy NPCs

    For any support related issues, to ensure the faster response possible, please use the official support channel:

    Support for the Enemy AI package

    This is the official forum for suggestions, comments, etc. Please fell free to provide any feedback.

    Thanks!
     
    Last edited: May 18, 2020
  2. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    The Enemy AI package figures a FSM (finite state machine) AI model for the enemy NPCs (non player characters).
    The main decisions are based on tridimensional senses, like the view sense, the hear and focus, etc. They are basically defined by a radius and, if applicable, an angle (ex.: field of view angle for the view sense):



    The following animations highlight some of the main states in the FSM. The patrol state is a clear alert level state, where the NPC guards a route or a fixed position:



    The search state is a warning alert state, where the NPC navigate to a suspicious position to investigate it:

     
  3. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    The take cover state is an engage state, in which the NPC seeks for a cover spot to protect itself from direct fire:



    There is a chance (customizable) for the NPC to avdance to a cover there is near the player, defining a more agressive approach:



    Finally, another enemy behavior that is important to highlight refers to the focus movements. This kind of behavior is triggered when the enemy is in close combat situation, making the NPC to focus on the target while moving, avoiding turning his back to the player:

     
    Fibonaccov likes this.
  4. MidnightCoffeeInc

    MidnightCoffeeInc

    Joined:
    Feb 28, 2017
    Posts:
    419
    This looks like exactly what I'm looking for!
    Before I purchase, my question is: Is there a way for me to modify the attack behavior?
    For certain enemies, is it possible for me to replace the animations to make it attack with a sword instead of shooting?
    Thanks!
     
  5. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    The actual attack action is basically a ranged attack behaviour, I don't know if it shares many code similarity with a melee type attack, but maybe it does, and you can take advantage of some part of code in the existing attack action.

    The point here is that, due to the modularity of the solution, you can easily create new states and actions to expand the FSM. In that case, you just need to create a new attack action for the melee attacks type.

    I am working on a next video on the tutorial series that will show how to expand the FSM with new behaviors. This will for sure provide a good overview for those who still haven't got the package (and also will clarify some stuff for those who already has it).
     
    Technobober likes this.
  6. MidnightCoffeeInc

    MidnightCoffeeInc

    Joined:
    Feb 28, 2017
    Posts:
    419
    Sounds fantastic, looking forward, thank you!
     
    Viniterra likes this.
  7. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Hello guys.

    A day 1 patch for the Enemy AI package is already online. It fixes some minor bugs regarding the enemy aim calibration, and also isolates scripts with common classes names under the Enemy AI namespace, to avoid conflicts.

    So please update your packages under the asset store.
     
  8. BioBurden

    BioBurden

    Joined:
    May 28, 2018
    Posts:
    103
    This looks great. Can we see some video examples of the AI in action in a FPS? Would love to see the AI take some tactical approaches to the player, beyond seeking cover (flanking, moving more often to avoid shots). Or how it behaves in a deathmatch scenario.
     
  9. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    The AI features some more than reactive actions, like engage in movement when closer to the target, advance to another cover near the player, using a tightening up strategy, evading a compromised cover, among others.

    A strategy for flanking, not by a single enemy but using divide and conquer, is being studied and are considered to be included indeed.

    Anyway, you can try it by yourself: Try the Demo

    The above demos are focused on the AI only. For a more professional experience, you can check the Third Person Shooter Bundle demo, which includes the Enemy AI package too: Try the Demo (here you can also download the binaries).
     
  10. BioBurden

    BioBurden

    Joined:
    May 28, 2018
    Posts:
    103
    Thanks for your reply. I understand that it's integrated with a third person controller, but what solutions are available for any currently first person assets? Thanks.
     
  11. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    I didn't understand well your question. You want to know if it is possible to integrate the Enemy AI within a FPS project? If so, yes it is.

    The Third Person Shooter Bundle is integrated with the Cover + Shooting System, a third-person controller. The Enemy AI contais 3 demonstration scenes: a FPS, a light gun and a top down shooter. This scenes in the standalone package are just to demonstrate the integration, and are focused on the Enemy AI features. The bundle contais a more polished, professional shooter controller, since it is a complete project.

    If you intend to integrate it with a third party player controller, the Enemy AI is you choice. Following the setup video tutorial, you can integrate it with any kind of shooter project, FPS included.
     
  12. BioBurden

    BioBurden

    Joined:
    May 28, 2018
    Posts:
    103
    Thanks for your reply, I'll definitely have to check the demo out.
     
  13. BioBurden

    BioBurden

    Joined:
    May 28, 2018
    Posts:
    103
    Hi, any further info on expanding the FSM of the AI you mentioned previously? Thanks.
     
  14. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    I am editing the video, adding audio for steps description, and it will come online as soon as I finish it, thanks.
     
  15. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    I'd like to use this in VR. Can the HUDs be omitted without breaking the system?
     
  16. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Nevermind, was too easy. I modified the Enemy AI package for VR (Oculus Quest w/ Link) with the XR Interaction Toolkit in about 30 minutes. This is a very nice, simple but powerful AI for Unity.

     
    Last edited: Aug 14, 2020
  17. AlexGLFC

    AlexGLFC

    Joined:
    Mar 28, 2019
    Posts:
    2
    Hey Viniterra, I've been building a VR game for a few weeks using your EnemyAI. Slow progress at times, but really enjoying using your asset (Thank you!!).

    I occasionally come across small little issues that I can't work out, especially as I'm not the greatest dev, if you could help me - that would be brilliant!

    I cannot work out how to change the parameters of the 'One shot audio' gameobjects that are generated for each gun shot. The volume is fine, but they quickly become quiet over distance, how can I change their 3D sound setting to have a greater minimum distance?

    And I have an enemy on a first floor, he will spot the player and react by running down the stairs to shoot, rather than shoot from where he is. Is there anything you can suggest?

    Thanks!
     
    Last edited: Aug 15, 2020
  18. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Nice demo! Aren't you using patrol way points for the enemies? They will sure guarantee a more dynamic environment.
     
  19. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172

    1) The shot is a 3D sound, where you can manage its volume and origin. The shot sound is triggered in the ShootBehaviour script, on the line 215 (the last parameter, with value 5, is the current volume):

    AudioSource.PlayClipAtPoint(weapons[weapon].shotSound, gunMuzzle.position, 5f);

    2) The enemy will try to look for a near cover to protect himself. If that cover is stairs down, and there is a navmesh path trough the stairs, the enemy will run down the stairs. The enemy also advances in cover towards the player eventually (the change cover chance parameter), so he may start by shooting the player from upstairs, and then try to change to a cover at the lower level. It is basically a composition of the enemy states in the FSM and the level design, specifically the covers and the current player positions.
     
    dgoodman-icf likes this.
  20. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    That was the FPS demo from the package so whatever was driving the AI there. I didn't change anything on the Enemy side except for the character bodies.
     
  21. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    This is prototype gameplay of the VR game (like Alyx - sort of) that I'm making for my son. It's just some gameplay to show features I'm working on with some prototyping levels using Enemy AI and XR Hand Poser. Each enemy has 2 waypoints that keep the player from just running down the middle. The area is very tight on the outer sides and the AI kept bumping into each other without yielding so I changed the original waypoints to prevent it. I'm not sure if this system has built-in functions for avoidance of other AI, but the NavMeshAgent does so easy to change later. I had to modify and write bridging scripts to get both of these plugins to work together in VR the way I want them to. I've got a lot more to do, but it's fun and the kid will like it.

     
  22. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    You have added new enemies, right? They are all in different positions. It seems you missed the patrol waypoints for this new enemies you added, so they are standing still like that.
     
  23. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Now I can see them moving!

    There is no intelligent avoidance right now, just the basic navmesh one. I'll think in a way to improve that, thanks for the suggestion.

    Anyway this game seems to be fun, nice work!
     
    Technobober and dgoodman-icf like this.
  24. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Oh yeah you're probably right. I was too excited to try it in VR and didn't add in the waypoints.
     
  25. kenmasu200x

    kenmasu200x

    Joined:
    Aug 15, 2020
    Posts:
    5
    Hi! I bought EnemyAI.

    and finished setup as your Youtube tutorial below.



    But... at the point 20:20 I played my scene but enemy doesn't work.

    My enemy just standing and do nothing.

    I want him to aim Player just like your video.

    I set up all this for 3 times.
    Aim Target , all stats file, Enemy Animator Controller,
    ,all prepared.

    but still not work.

    As a test, I changed Current State.
    just like..
    DebugCarlbrateState -> AttackState

    And enemy moved for the first time!

    But... He just turned 12 o'clock direction, and keep shooting ground.
    ignoring Player...


    What's wrong with this?
    if you have some idea , pls tell me.

    (Unity version 2019.4.8f1 Personal)
     

    Attached Files:

  26. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Did you set your player's Tag and Layer both to "Player"?
     
  27. kenmasu200x

    kenmasu200x

    Joined:
    Aug 15, 2020
    Posts:
    5
    Hi
    yes.... for sure.
    both are Player.

    All layers are children included.

    my enemy keep shooting the ground without reload. (no limit bullets)
    It's curious
     
    Last edited: Aug 27, 2020
  28. kenmasu200x

    kenmasu200x

    Joined:
    Aug 15, 2020
    Posts:
    5
    After a few hours , It work well!
    Enemy is aiming Player. but sometimes doesn't work.
    I don know why.

    I got this error, maybe some trouble around NavMesh.
    I'll try around it. thanks
     

    Attached Files:

  29. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172

    As the error informs you, have you added the navmesh agent component to the enemy, and baked the navmesh on the ground?
     
    dgoodman-icf likes this.
  30. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    How or where do you set the enemy shot raycast ignore layer? Bullet marks are showing up on my controller collider dangling in mid air.
     
  31. kenmasu200x

    kenmasu200x

    Joined:
    Aug 15, 2020
    Posts:
    5
    yes! I forgot to bake the ground. and everything works for now!
    thank you for your answer.
     
    Viniterra likes this.
  32. TabArtwork

    TabArtwork

    Joined:
    Jun 9, 2020
    Posts:
    5
    Hello. This system looks really cool!
    I was wondering if this system would also work for friendly NPCs that attack enemies instead of the player?
    I understand it may require adding new states and some set up, but I just wanted to know if its potentially possible to do with this asset.
     
  33. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    You may be able to clone the EnemyStats asset and change the target mask to Enemy. I haven't tried it though.
     
    Viniterra likes this.
  34. AlexGLFC

    AlexGLFC

    Joined:
    Mar 28, 2019
    Posts:
    2
    Thank you!
    I'll try manipulating some cover to be in front of him upstairs. For example some invisible blocks below the window and see how that works. I'm happy to for him to change his position, but I'd rather he stayed where I place him for a small while before he does.
     
  35. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    You must include your player layer into the Target Mask of the EnemyStats asset. Beware that it must be the layer of the Game Object containing the collider.
     
  36. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    I have already mentioned on an early response about a next video tutorial on that matter. Unfortunately, the current moment made me delay that video production, but it will still be released in the future.

    Basically, this new video tutorial will show how to extend the FSM, and in fact it shows how you can even create a new kind of NPC, with an entire new FSM. There, I will create a hostage type NPC, with some basic behaviours like follow the player, protect in cover against enemies (using the target mask).

    I believe it may be useful to anyone who wants to better understand the entire system, and provide a base to create other types of NPCs, like the companion you mentioned.
     
    Last edited: Sep 5, 2020
    kikasik and dgoodman-icf like this.
  37. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    You mean stay more time on the same cover? If so, you can reduce its change cover chance parameter, on the specific ClassStats asset.
     
  38. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    The player (in layer Player) is in the Target Mask already and the collider is on that layer. The bullet hit decals still show up on the player collider.
     
  39. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Are there any other colliders inside the player?
     
  40. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Yes in the "Player" layer as well, though there could be instantiated items at times with other layers. Think that's the problem? I can't change those layers we'd need your plugin to be more flexible if that's the case.
     
  41. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Capture.JPG
     
  42. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Could you contact me trough the support form, so I can assist you better? Thanks.
     
  43. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    WIll in the morning. Thanks
     
    Viniterra likes this.
  44. TabArtwork

    TabArtwork

    Joined:
    Jun 9, 2020
    Posts:
    5
    Thanks so much!
    I ended up going purchasing this asset and I'm loving it! Already left a 5 star review.
    Thanks for the support and the great asset!
     
    Viniterra likes this.
  45. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    Glad to hear you liked the package, thank you for the feedback.
     
  46. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Desktop 2020.09.17 - 11.09.36.11.4066.Still003.png Here's some more EnemyAI game play from my game

     
    Last edited: Sep 18, 2020
    Viniterra likes this.
  47. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    I figured it out:

    In the attack script (and other places) you're finding objects by referencing its root object:
    Code (CSharp):
    1. bool isOrganic = ((1 << hit.transform.root.gameObject.layer) & controller.generalStats.targetMask) != 0;
    But at certain times, my Player controller is temporarily reparented to another object (platforms, etc). I've edited them to change root to parent and it works much better now. Same goes for enemies, you can't group them under an empty game object in the hierarchy panel because the scripts will think the empty object is the enemy object root.
     
  48. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172

    Nice to know you've figured it out.

    The root has to be checked because of ragdolls, where the collider is in child game objects. This way makes possible to make an AI engage with another one, like companions versus enemies.
     
  49. dgoodman-icf

    dgoodman-icf

    Joined:
    Jan 7, 2019
    Posts:
    36
    Yeah I see why you're doing it in this case, but what about the alertManagement script? The alertMask here is for "Enemy" ( 1<< 12), but in this code it won't work because the colliders in the overlap sphere (bones) aren't in the Enemy layer and we're not checking their root or parent.

    Or is this intended for something else? I've done a lot of testing and I see the alert ping, but I never see AlertNearby sending the "AlertCallback" message.
     
  50. Viniterra

    Viniterra

    Joined:
    Dec 4, 2014
    Posts:
    172
    On the AlertNearby function, check this line:

    obj.SendMessageUpwards("AlertCallback", target, SendMessageOptions.DontRequireReceiver);

    The obj is any collider detected within the radius. In the enemy case, it is a member of the ragdoll, a child of the root game object. The SendMessageUpwards calls for the AlertCallback function on a parent game object, if any (DontRequireReceiver). The receiver is the StateController script, present in the root of the enemy, that implements the AlertCallback function.

    For more info, you can check the documentation for the Unity SendMessageUpwards function.

    EDIT.: there is a trick I have used in order to have just one collider per enemy detected: the Hips bone has the enemy layer also.