Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tactical Shooter AI - Asset Store Pack

Discussion in 'Works In Progress - Archive' started by squared55, Mar 2, 2015.

  1. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818

    Like I said, you need to attach the AI Controller to an empty gameobject.

    A layermask refers to a variable on the script. You can select multiple layers. Select the appropriate layers on each script.
     
  2. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130


    as you see
    I make a empty game object and its not child to any thing!

    but there is no Ai controller to add !

    Am I doing Right ?
     
  3. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    It is just called Controller (Script). Try manually looking for it in the C# AI folder.
     
  4. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    Ok it worked!
     
    Last edited: Jun 8, 2016
  5. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    @squared55 watch this :


    this is a riffle agent !

    and about the layers!
    when I go into that building , the agent shoots at me(his bullet hit the wall)! and also he dont go for covering!he just stay in my last position (spawn point) and try to shoot at me (as you see in video)!
    can you explain this in amateur level?
    step by step!
    this might be my last noobish question!
    // in demo scene all the level parts are in default layer! and i set all layers to default in my scene! but still agent try shoot me when I am inside building !
     
    Last edited: Jun 9, 2016
  6. sheffieldlad

    sheffieldlad

    Joined:
    Oct 9, 2013
    Posts:
    154
    Yes if we can override methods from the gun script in future updates. (I'm only really learning c# but I don't think we can currently.)
     
  7. f1chris

    f1chris

    Joined:
    Sep 21, 2013
    Posts:
    335
    Exactly what I wanted to hear from you !! :) thanks for the heads-up !

    I'll be able to keep the Cover Nodes who are less CPU heavy than the dynamic cover.
     
  8. musolo

    musolo

    Joined:
    Sep 12, 2014
    Posts:
    238
    How much this will save performance percentage wise?
     
  9. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    This does work for about 2 seconds and then the AI wanders off... even if I shoot again they wont be bothered to advance towards me again(if im not in LOS).... any ideas?
     
    Last edited: Jun 9, 2016
  10. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Add a cover node in the building. Also, if you are close enough to the wall, they will still shoot at you for suppressing fire unless you lower the setting in the gunscript's dist allowed in front of target variable.

    And add a navmesh obstacle to your player.

    1. Find script on object or prefab.
    2. Find layermask variable in inspector.
    3. Open dropdown.
    4. Pick layers.

    Did you set the AI Controller's layermask to include this layer?

    That is different for each case, depending on where the settings start and where they end up. The best way to check for your project would be to test it yourself. :)

    Decrease the targetScript's time between reacting to sounds variable. Decrease the base script's radius to call off search.
     
    RangePlusOne likes this.
  11. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    watch this:

    Edit: the problem of agent was able go trough the walls solved ! I didn't bake navmesh for building! I just bake for cube(ground)!
    but still the agent just come and shot me and dont go for cover!
    Edit2: I put the agent in the building , but he is able to detect me behind the walls!!(and still dont go for cover even the covernod is next to him!)
    how?
     
    Last edited: Jun 10, 2016
  12. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Again, make sure your layermasks are properly set up, including those on the cover nodes. There's a section in the manual on setting up cover maps too. You need to have the hide spot behind a wall and the firing spot have a clear line of sight to the player.

    I'd strongly recommend looking at the demo scenes and watching the agents in editor view to see how they interact with the nodes. Also use a waist high wall for testing purposes on cover.

    Component>Navigation>Nav Mesh Obstacle
     
    Last edited: Jun 10, 2016
  13. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    ufps player - Agent - wall - ground - AI controller - Cover nod - options in AI Controller and Cover nod!
    all of them are set to "default" layer!
    Also as demo scene I make Cover nodes child of game object which you said to give ai "controller" script !
    //
    will you make a video tutorial ? please ! I need this! just a agent and a cover nod and a wall !
     
    Last edited: Jun 11, 2016
  14. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    in video you just add a cover nod - and I do same as you and demo scene!
    will you make a video just for covering?
     
  15. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I don't think there's anything else to say on the topic that I haven't said already. :)

    I can only assume you're having an issue with the layermasks. Make sure you're setting the layermask script variable and not the object's layer, BTW.
     
  16. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    this is only 30 sec ! please watch it and tell me what I missed and where was wrong?

    thank you
     
  17. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    What are your NPCs behavior set to? I use tactical and they cover their asses like crazy. You also want to makes sure those cover nodes are leveled appropriately, like they actually cover the npc's lower body properly.
     
  18. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    I've got a question - the AI don't seem to notice me unless I literally jump in front of them? The target script is in the camera and I recently tried placing it in the head to make sure it's apparent, but no luck. Here's an example: https://gyazo.com/444f27d6bfd9d87d7dbb840b36ff5b4e any ideas? After I jump, they target me - but it's as if because they don't have a path to me initially, they don't see me....

    Also I have suffering a catastrophic lag spike when I shoot the shotgun from the SoundMaker script, it literally freezes the game as seen here... Really don't understand why this would generate a FPS drop that makes it unplayable? Anyone have a solution?

    UPDATE: I placed the Sound Maker script on the shell of the shotgun bullet, so now it only spawns 1, not 6 or 12, it's much smoother now. BUT npcs will still make the scene lag a bit from all their shooting (individual bullets like the rifle) I'm all ears.
     
    Last edited: Jun 12, 2016
  19. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    it is set to tactical!
    is it important to cover finding? can explain more?
    the distance from cube which they cover behind it? the height from ground?
    what is the value in default?
    @RangePlusOne can you make a short video about setting covers?
     
    Last edited: Jun 12, 2016
  20. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    This is what I've done, this kind of height works. https://gyazo.com/3916badaff0eada7f17856568a9bc183
    I haven't modified any cover value either. The barrier is static as well so be certain your objects are as well.
     
  21. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Hi,

    I'm just getting started with Tactical Shooter AI in UFPS. The Enemy AI will sometimes run underneath the UFPS player. The player ends up standing on top of the AI's head. How do I prevent this?
     
  22. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    I have the same problem and as you see older posts Im trying to solve this! read them and check you can handle his problem !
     
  23. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    Add a Nav Mesh Obstacle component, slap it on the player - that should do it.
     
    Tinjaw, TonyLi and NeoUnity like this.
  24. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    I think I solved the problem! everything I set was correct! problem was the position of cover nod! they should be 0.5 upper than ground and in the navmesh area! I set them in 0 height and in gray area which character cant go!
    now a question , how can I reduce the target finding of AI ? Ai can detect me from long distance !

    If you are agree I delete my old posts and make a post about Integration?
     
    Last edited: Jun 12, 2016
  25. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Thanks! That helped quite a lot. I'm just digging into Tactical Shooter AI, so I haven't set up any kind of cover information in my test level yet. I suspect that should probably clear up the rest of the issue.
     
  26. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I imagine there's a collider getting in the way. Try setting the controller's layermask to nothing, if they see the player, that's the issue.

    As for AI's making sounds, they will make a "shout" when they spot a target, so sounds on their bullets are kind of redundant.

    The lag looks like it's coming from something printing to the console. Not unexpected, but I haven't been able to find the offending line in my version. Perhaps I already fixed it. I'll keep looking. :)

    Under line of sight parameters in the target script you can change the maximum dist to notice the target.
     
    Last edited: Jun 13, 2016
  27. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    But if I'm behind the enemy, I shoot around them (not hitting them because I have been known to be a bad shot) they won't react.... right??
     
  28. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    You'd make the player use different bullets that do make sounds.
     
  29. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    Like whizzing, bullets zipping past sounds?
     
  30. arnesso

    arnesso

    Joined:
    Mar 3, 2015
    Posts:
    96
    @squared55
    - I would need help to damage agent with a thrown crate, have you ever worked on similiar like this ? (
    So I grab a box, or crate and I throw the box toward the agent he receives damage)
    -Will there be any solution to we will be able to grab/ throw/ push / lift the AGENTS as Rigidbody and fall damage or if we throw the agen to the wall the agent recives damage ?
    I also would be glad any tip on which way should I start this stuff .
    Thank you :)
    Cheers ! :)
     
  31. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I was thinking just adding the SoundMajker script, but yeah, that too. :)

    1. I've never done it, but what I suppose I'd do is when the object enters a collision you'd take the velocity, multiply it by the weight, and then do that much damage to the agent with the same methods as the bullets.

    2. Ragdoll-while-alive is something I'd like to add at some point in the future, but I've got nothing beyond that.
     
  32. RangePlusOne

    RangePlusOne

    Joined:
    Dec 6, 2014
    Posts:
    123
    I do that though... I added the sound maker script to each bullet so the enemies know when the bullets are around? I'm confused now... I do have separate bullets for the player too.
     
  33. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Only add them to the player bullets. Or the player muzzle flashes, assuming that's a prefab. But you'll still get lag unless you comment out whatever in the sound system keeps writing to the console (I still haven't found anything that could be the source of it). What version of the AI are you using?
     
    Last edited: Jun 17, 2016
  34. sheffieldlad

    sheffieldlad

    Joined:
    Oct 9, 2013
    Posts:
    154
    Hi @squared55

    I've made a set of spawn points for AI agents which works great but I noticed a lag spike each time one is instantiated.
    On further investigation it's not the actual instantiating of the prefab which is directly causing the problem.
    This line in the agents cover script is the culprit.

    Code (CSharp):
    1. verts = navI.GetNavmeshVertices();
    which calls
    Code (CSharp):
    1. NavMesh.CalculateTriangulation().vertices;
    in my case this returns >800k vector3s which causes quite a lag spike.
    I've changes the code a little so I call NavMesh.CalculateTriangulation().vertices; only once at the start of the game and stored the result in an array of vector3s called vertarray then my AI agents cover scripts get a copy of the vertices array from vertarray.

    Do you see any harm in doing it that way if I'm using a cover map and not using dynamic cover?

    Also, what does the line

    Code (CSharp):
    1. verts = verts = navI.GetNavmeshVertices();
    do?
    Why vert = verts = .....

    I'm not familiar with that syntax (I'm a novice coder :) )

    Many thanks.
     
  35. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Should be perfectly fine.

    I just messed up. :)
     
  36. musolo

    musolo

    Joined:
    Sep 12, 2014
    Posts:
    238
    Please man make video on this topic if you can)
    This spawner thing is important part of gameplay and many guys would be greatfull to you for covering that, including me!
     
  37. sheffieldlad

    sheffieldlad

    Joined:
    Oct 9, 2013
    Posts:
    154
    I don't have time to do a video but I'll share the code and my method here.
    Please note, This setup is for the type of game I'm making.
    It might not suit your game.
    It works well for me. your mileage may vary.

    Just to give context:
    The level in my game this code is pulled from is a survival level.
    The player is playing to beat their highest score and also see how long they can survive.

    Every 3 seconds a spawn point is chosen at random and an enemy is spawned while there are less than 30 enemies in the level.
    If there are 30 enemies in the level, no new enemies are spawned.

    Each time an enemy dies, another is spawned 3 seconds later until the maximum number of 30 is reached again.

    The only way this level ends s if:
    a) the player quits.
    b) the player dies.



    First off, I created an empty game object and named it Spawner. and added this code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawner : MonoBehaviour {
    5.  
    6.     //Drag your enemy prefabs into here in the inspector.
    7.     public GameObject[] Enemies;
    8.  
    9.     //this is better than having the garbage collector scrap a temp variable each time Spawn() is called.
    10.     public int counter = 0;
    11.  
    12.    
    13.  
    14.  
    15.  
    16.     //Call spawn from another script or however you like.
    17.     public void Spawn(){
    18.         counter = Random.Range (0, Enemies.Length);
    19.         GameObject enemy = (GameObject) Instantiate (Enemies [counter], this.transform.position, Quaternion.identity);
    20.  
    21.    
    22.     }
    23. }
    Save your spawner game object as a prefab then copy as many as you need into your scene, position them where you need and add the AI Agent prefabs to each Enemies slot in the inspector.

    I use a game manager script to call Spawn () on each different spawn point.
    Every 3 seconds my game manager script chooses a spawn point at random. The chosen spawn point spawns a random enemy.

    Part of the code looks like this....

    Code (CSharp):
    1.  
    2. //variables.
    3. public GameObject[] Spawners;
    4. public float timer = 0;
    5. Spawner spawnScript;
    6. //how often do we spawn an enemy?
    7. public float timeToSpawn = 3f;
    8.  
    9.  
    10.  
    11. //this goes in update
    12. //add the time since the end of the last frame to the timer variable.
    13.     timer += Time.deltaTime;
    14. //if 3 seconds have passed...
    15.                 if (timer >= timeToSpawn) {
    16. //reset the timer....
    17.                     timer = 0;
    18.                     //if we have less than 30 enemies in the level,  choose a spawn point and spawn a new enemy.
    19.                     if (enemiesInScene.Count < 30) {
    20. //choose a random spawn point
    21.                         int i = Random.Range (0, spawners.Length);
    22. //get a reference to the spawn points Spawner script
    23.                         spawnScript = spawners [i].GetComponent<Spawner> ();
    24. //spawn.
    25.                         spawnScript.Spawn ();
    26.                     }
    Drag your spawn points into the Spawners array in the inspector and you should be good to go :)
     
  38. arnesso

    arnesso

    Joined:
    Mar 3, 2015
    Posts:
    96
    hi

    I have tried to write script which influences the AGENT can see me or not. ( Hiding in the bush) ,But TargetScript does not change any value . Right now this is what i have : ( I know there is a lot of unnecessary stuff, i just tried many things. )
    Any idea.?
    I guess the best way would be if the agent lost awerness, or the "max distance to notice target" will be only 2, in case of exit collider the value would return;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class HidingZone : MonoBehaviour {
    6.  
    7.     public Color boxColor = Color.green;
    8.     private ParagonAI.BaseScript paragon;
    9.  
    10.     // Use this for initialization
    11.  
    12.     void Update (){
    13.         GameObject target = GameObject.FindGameObjectWithTag ("Enemy");
    14.  
    15.     }
    16.     void OnTriggerEnter(Collider other)
    17.     {
    18.         if (other.transform.tag == "Player") {
    19.             paragon.EndEngage ();
    20.  
    21.         }
    22.     }
    23.     public void OnTriggerStay(Collider other)
    24.     {
    25.         GameObject target = GameObject.FindGameObjectWithTag ("Enemy");
    26.         if (other.transform.tag == "Player") {
    27.             paragon.EndEngage ();
    28.  
    29.             target.GetComponent<ParagonAI.TargetScript> ().currentEnemyTarget = 0;
    30.          
    31.         }
    32.      
    33.     }
    34.     void OnTriggerExit(Collider other)
    35.     {
    36.         if (other.transform.tag == "Player") {
    37.             paragon.StartEngage ();
    38.  
    39.         }
    40.  
    41.      
    42.      
    43.     }
    44.     void OnDrawGizmos()
    45.     {
    46.         Gizmos.color = boxColor;
    47.         Gizmos.DrawCube(transform.position,GetComponent<BoxCollider>().size);
    48.      
    49.     }
    50.  
    51. }
     
  39. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    You just need to adjust the targetscript's targetPriority variable to a negative value to make it lose awareness. You also need to call

    Code (CSharp):
    1. ParagonAI.ControllerScript.currentController.UpdateAllEnemiesEnemyLists();
    To make sure the agents register your changes.

    Calling EndEngage or StartEngage will have virtually no effect as the AI will just override your changes soon after as they spot the target again or still don't have sight of it. Same deal with overwriting the target.

    Look at the adjust target priority script I put on the exploding barrel prefab for reference.
     
    Last edited: Jun 20, 2016
    sheffieldlad and arnesso like this.
  40. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Since there's 19 pages to wade through to look for an answer ill ask here, sorry if it been covered - does this work on procedurally generated levels? I'd like to use a pathfinding asset that can generate a navmesh at runtime
     
  41. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,676
    I believe the answer is going to be that you'll need to do your own navmesh hookup. This AI is built on Unity's navmesh.
     
  42. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    It uses the Unity navmesh by default, so no out of the box. However, if you're willing to open up the Navmesh Interface class and extend or modify the code to work with your other asset, it should work fine. Here's the class in question, you'd need to change the body of all the methods:

    Code (CSharp):
    1. namespace ParagonAI{
    2. public class NavmeshInterface : MonoBehaviour
    3. {
    4.     NavMeshAgent agent;
    5.  
    6.     public virtual void Initialize(GameObject gameObject)
    7.     {
    8.         if(gameObject.GetComponent<NavMeshAgent>() != null){
    9.             agent = gameObject.GetComponent<NavMeshAgent>();
    10.         }
    11.         else
    12.         {
    13.             Debug.Log("No Agent Found!");
    14.         }
    15.     }
    16.  
    17.     public virtual void SetDestination(Vector3 v){
    18.             agent.SetDestination(v);
    19.     }
    20.  
    21.     public virtual bool ReachedDestination(){
    22.         return (agent.remainingDistance != Mathf.Infinity && agent.pathStatus == NavMeshPathStatus.PathComplete && agent.remainingDistance <= 0 && !agent.pathPending);
    23.     }
    24.  
    25.     public virtual bool PathPartial(){
    26.         return (agent.pathStatus == NavMeshPathStatus.PathPartial);
    27.     }
    28.  
    29.     public virtual Vector3 GetDesiredVelocity()
    30.     {
    31.         return agent.desiredVelocity;
    32.     }
    33.  
    34.     public virtual bool PathPending(){
    35.         return agent.pathPending;
    36.     }
    37.  
    38.     public virtual bool HasPath(){
    39.         return agent.hasPath;
    40.     }
    41.  
    42.     public virtual Vector3[] GetNavmeshVertices(){
    43.         return NavMesh.CalculateTriangulation().vertices;
    44.     }
    45.     public virtual void SetSpeed(float f){
    46.         agent.speed = f;
    47.     }
    48.  
    49.     public virtual float GetSpeed(){
    50.         return agent.speed;
    51.     }
    52.  
    53.     public virtual void SetAcceleration(float f){
    54.         agent.acceleration = f;
    55.     }
    56.     public virtual float GetAcceleration(){
    57.         return agent.acceleration;
    58.     }
    59.     public virtual void SetStoppingDistance(float f){
    60.         agent.stoppingDistance = f;
    61.     }
    62.  
    63.     public virtual float GetStoppingDistance(){
    64.         return agent.stoppingDistance;
    65.     }
    66.  
    67.     public virtual float GetRemainingDistance(){
    68.         return agent.remainingDistance;
    69.     }
    70. }
    71. }
    72.  
     
    lazygunn and hopeful like this.
  43. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Thanks for the answer! I may give it a go
     
  44. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    do you have any plan for |Zombie AI| ?
     
  45. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    I've thought about it. Haven't done much more than that, though.
     
    Last edited: Jul 2, 2016
  46. rbm123

    rbm123

    Joined:
    May 24, 2015
    Posts:
    130
    Hi -
    Should I update package or I can Integrate RFPS with my current version(v1.11)?
    if yes will you send document ?
    **if you remember I told you I have 3 page manual!
     
  47. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    You will have to update your package. The instructions are a separate text file, but you should really contact Unity over that manual issue. What pages does your manual have?
     
    Last edited: Jul 2, 2016
  48. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    @squared55

    I am driving myself crazy. The AI always knows where my player is. I have set all the buildings into a building layer and added that to the AI Controller's Layer Mask. I even went so far as to set the mask to Everything. The AI still sees me. The follow the NavMesh to get there, but they are seeing me through buildings. I really need some help with this. What are your suggestions?

    update

    I can not reproduce this in a simple test scene. Which I figured would be the case. So, it probably isn't a Tactical Shooter AI issue, but a more general problem. I could still use some help because I don't know what is different about my main scene and the test scene.

    update 2

    I have put a break point in TargetScript line 452
    NoticeATarget(enemyTargets);
    And the AI is not *seeing* the player, yet it is moving towards my player. My player is across the map and not moving. How else can the AI know where to find my player?
     
    Last edited: Jul 12, 2016
  49. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    If you're using the Search Idle behavior, they'll home on the player's area. This is intended behavior to get combat started quickly when an agent is spawned. If switching that behavior out (for, say, Wander or Patrol) doesn't work, can you tell me whether or not the agents raise their guns and point them at the player right away?
     
  50. Tinjaw

    Tinjaw

    Joined:
    Jan 9, 2014
    Posts:
    518
    It seems that I misunderstood what search was. Your reply has clarified things for me. I will now use it accordingly with wander. Thanks for the reply.