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

AI Behaviors Made Easy! (Walker Boys)

Discussion in 'Assets and Asset Store' started by profcwalker, Apr 22, 2012.

  1. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    My Idle state use's No Player in Sight -> Change To State: Patrol

    So i duplicated Idle and renamed the Mono public Class Idle1 : BaseState, Then went into Advance Mode in the the Inspector AIBehaviours Script and added Idle1, then directed my GotHit state to Idle1. Then followed most of you''re lead set in the above suggestion which worked out fine. :)

    Thank You for the Support Nathan
     
    Last edited: Sep 27, 2012
  2. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Awesome, that's great to hear :)

    I'm not sure when we'll have the next update submitted, but you shouldn't have to jump through this small hoop within the next few weeks.
     
  3. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    :) I updated the Game Demo and included a OSX version (Hope it works! )

    Forum Link
     
    Last edited: Sep 28, 2012
  4. ineteye

    ineteye

    Joined:
    Sep 12, 2012
    Posts:
    35
    Hi! find some issue that Patrol Points do not stores on prefab... but i need to spawn multiple AI from spawnpoint and then they should patrol to waypoints...
     
  5. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    what i did was drop prefab patrol_points_group_1 into the scene from the project folder and made sure the avatar's inspector PatrolState Movement Options "patrol points group" was actually assigned from the scene in the avatars inspector.

    though on one occasion i think , Was having trouble with the Patrol Points prefab It might have gone away when i baked the navmesh ?

    Continue Previous Patrol is available and would effect the avatars ability to reach patrol points sequentially.
    $PatrolMode.jpg
     
    Last edited: Sep 30, 2012
  6. ineteye

    ineteye

    Joined:
    Sep 12, 2012
    Posts:
    35
    The problem that as i wrote... ai spawn from prefabs goes to waypons and destroying when go away from view... like cars in GTA... and its not possible to put them all on scene... so they have to be in prefabs.... at least maybe their is a way to fill this field after spawn throught line of CS script??
     
  7. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    not the best way to handle the problem in my eye's

    If it was a large map like GTA i could imagine doing something like this: spawn the AI once and set starting and ending waypoint locations on the streets, then move the waypoint's and avatar positions (start end ) depending where the player is in location to zones or area triggers and save the Instantiates for lighter things like projectiles and particle effects.
     
    Last edited: Sep 30, 2012
  8. ineteye

    ineteye

    Joined:
    Sep 12, 2012
    Posts:
    35
    unfortunately it is not like in GTA... it was just compare... it spacebase (or better say space station)... and it have docks... from this docs coming space ships in both ways... some of ai just do something around a base....
     
  9. runner

    runner

    Joined:
    Jul 10, 2010
    Posts:
    865
    why is it unfortunate? being on a road or in space unity see's the objects as Vector3 position at least as far as we are concerned with its waypoint's, If you want to get rid of a ship AI that has just docked and replace it with a static version then just move the real AI ship to some-other start location for rinse repeat. but if it went into patrol / attack then likely would use some other states.

    Just thinking if it might be useful to be able to change waypoint group prefab in the avatar patrol state using a index gameObject array triggered from some base state used for that purpose. Just for ease of development without devising alternative control scheme's like was mentioned above.
     
  10. ineteye

    ineteye

    Joined:
    Sep 12, 2012
    Posts:
    35
    OK ))) I will think about it ))
     
  11. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi ineteye,

    What runner is saying about keeping the vehicles alive in the world and not destroying them is the best way to handle this so that you don't incur a performance hit when spawning and destroying objects. If you're worried about rendering and such, you can simply do a GetComponentsInChildren call for the Renderer component and shut off the renderers until you want it to be visible to the player again.

    The API, which you can already get to mostly, is not completely finished and something that I just now added because of you mentioning this on this post :) was a code-able way to set the patrol points.
     
  12. Mementos

    Mementos

    Joined:
    Sep 25, 2012
    Posts:
    79
    Delete
     
    Last edited: Oct 4, 2012
  13. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Just purchased AI Behaviors. I don't see any info about how character movement is handled. There's no character controller required, and when I add one it doesn't do anything. In the "Follow" state, the example avatar rotates toward the target, but doesn't move forward. I'm probably missing something simple, I'm having the same issue when adding the AI behavior components to my own characters as well. They rotate, but don't move. FYI, I'm adding the AI Behaviors script without the NavMesh component because I'm using Granberg's Pro pathfinding system. I haven't yet scripted for that. First I'm trying to get the out-of-the-box movement to work without pathfinding just to get used to the system. Thanks for the help.
     
  14. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi tool55,

    You'll need to script for the Astar system before the character will move simply because that is the means by which it moves. It's pretty easy to implement. The following videos are around 25 minutes because I go through the whole process.

    Part 1
    Part 2

    Hope they help,
     
  15. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Thanks, Nathan. So I take it there's no built in movement scripting. That has to be handled by either the pathfinding scripts or else I would have to write a basic movement script for the character controller based on the character speed variable in your AI Behaviors editor. Is that correct?

    I did watch the videos and wrote the C# script to pass the target variable to the pathfinding script. I can see how that works for the patrol state, but what about a simple "follow" state? I mean, the target in that case is any nearby game object with the "Player" tag. So how do you pass that to the pathfinding script from your AI Behaviors scripts? I'm a bit new to C# programming, so please excuse the questions.
     
  16. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi tool,

    How pathfinding solutions tend handle movement is by simply getting a destination coordinate and calculating a new path internally. So we end up handling the actual behavior of the movement within the states themselves, which call the main AI Behaviors script, which in turn calls the method you assign in your script because you told the main AI Behaviors script that it was the method you wanted it to call.

    So as a quick example, the following is the basic flow of the AI character when you provide a callback as in the example video:

    Patrol State --> AI Behaviors Main Script --> Your Script --> Astar
    Follow State --> AI Behaviors Main Script --> Your Script --> Astar
    Flee State --> AI Behaviors Main Script --> Your Script --> Astar
    etc...

    I'm not sure if that explains it. I think the easiest way to explain is that the States themselves handle the movement behavior, our main AI Behaviors script in turn tells the Unity internal pathfinding solution what that position was, unless you provide a callback, in which case your script tells the 3rd party pathfinding system what that point was whenever a new position is available.

    Please let me know if that answers your question,
    Nathan
     
  17. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    I think I've got it. So I just need to pass a position (destination) to the Astar script through my script. I guess I was hoping that one small script would handle the task of communicating with the astar script, that there was a common variable (say "target") that would work for patrol, follow etc. In the patrol example, the AI Behaviors script moves the target around and communicates that position to the Astar script. I was hoping that a game object with the player tag could be assigned to the target variable in the follow state. Perhaps that's one way to go. But looks like different states need their own scripts. Anyway, I'll figure it out, just have to stare at it for a while. I'm assuming that with Unity's built-in pathfinding none of this is necessary. The Navmesh component handles this communication. (A good reason to upgrade to Unity Pro). Thanks so much for your help. Your example makes it clearer.
     
  18. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    So this will be available in the next release? Or can we get to it now...?

    I've got a game in which I want to send waves of enemies in (say, 5 at a time). So to do that, Instantiating a prefab at a spawn point makes sense (rather than having 50 enemies working away with their Renderer turned off). But, as pointed out, when Patrol Points is populated in the Prefab, it doesn't stay populated when that prefab is instantiated.
     
  19. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi nm8shun,

    I thought I had replied to your post, but I guess I didn't after all. We will more than likely be submitting the latest version tomorrow, so you should have it within a few days.

    If you need it now, you can update the following code in the PatrolState script...

    1) Rename the GetSortedPatrolPoints method to SortPatrolPoints

    2) Add the following method

    Code (csharp):
    1.     public void SetPatrolPoints(Transform patrolPointsGroup)
    2.     {
    3.         if ( patrolPointsGroup != null )
    4.         {
    5.             this.patrolPointsGroup = patrolPointsGroup;
    6.             SortPatrolPoints();
    7.         }
    8.         else
    9.         {
    10.             patrolPoints = null;
    11.         }
    12.     }
    13.  
    Hope this helps :)
     
  20. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    Perhaps you already know this but AIBehaviors.cs line 450 returns an error if the game speed is ever 0. Something about Rotation can't equal "NaN, NaN, NaN, NaN". I added " Time.timeScale > 0" to the if statement above it and the errors went away.
     
  21. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Thanks so much Drew, we really appreciate you pointing this out to us :)
     
  22. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    Hi,
    I have some problem with A* integration so I hope same one can help me. I did everything as video tutorial says but I have this error when is AI Behaviors set to "Seek":
    NullReferenceException: Object reference not set to an instance of an object
    AI_PathResponder.OnMove (Vector3 targetPoint, Single targetSpeed, Single rotationSpeed) (at Assets/AI_PathResponder.cs:33)
    AIBehaviors.MoveAgent (Vector3 targetPoint, Single targetSpeed, Single rotationSpeed) (at Assets/AIBehavior/Plugins/AIBehaviorsMadeEasy/AIBehaviors.cs:406)
    AIBehaviors.MoveAgent (UnityEngine.Transform target, Single targetSpeed, Single rotationSpeed) (at Assets/AIBehavior/Plugins/AIBehaviorsMadeEasy/AIBehaviors.cs:382)
    SeekState.Action (.AIBehaviors fsm) (at Assets/AIBehavior/Plugins/AIBehaviorsMadeEasy/States/SeekState.cs:80)
    BaseState.HandleAction (.AIBehaviors fsm) (at Assets/AIBehavior/Plugins/AIBehaviorsMadeEasy/States/BaseState.cs:140)
    AIBehaviors.Update () (at Assets/AIBehavior/Plugins/AIBehaviorsMadeEasy/AIBehaviors.cs:194)

    Iam using danreid70s script from 13page and when I set AI to seek cube, script create TARGET_Player object on cube position and also I see this object assigned to AI_PathResponder script on AI in editor. AI just randomly walk/run around cube sometime even not that and run some far away. I have free version of A*.
    Hope you understand me and thank you for help.
     
  23. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi matis,

    If at all possible, can you email me your project and I can take a look at it to help you find a fix to the problem. Our email will be pulled up if you go to the AI Behaviors menu and choose "Report a bug".

    Thanks,
    Nathan
     
  24. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    Hi,
    can you please give me your email address? Today I have one more error and also when I click to "Report a bug" nothing is happened.
     
  25. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    I'm having some difficulty with the A* integration as well. I used the PathResponder script from the tutorial video modified for the AIPath script in the Pro version of the A* project. The patrol points work perfectly and the AI follows them as expected. But when I try to switch to to fhe "follow" state, the AI just keeps running around the patrol points. In other words, I have the follow state set to switch on when the player is within 30 units, but when I move within range, the AI doesn't respond. Do I need to script that or does AI Behaviors automatically switch the target to the objects tagged as "player" that come within range. Is that PathResponder script supposed to be universal for all of the states: seek, follow, flee, etc.?.
     
  26. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Tool,

    The most common issue would be if you have a collider/trigger on your player or your AI and the layer it is on is not be turned off in your AI's Obstruction layers. In other words... make sure any colliders or triggers on your AI and on your player are assigned to a layer, and then be sure to uncheck those layers on your AI.

    To answer your other question, yes, the PathResponder is universal to all states, so that should not be your issue.

    Please let us know if the above doesn't solve your issue and we can go from there.

    Hope this helps,
    Nathan
     
  27. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Thanks much. Any news on the new update?

     
  28. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    Hi,
    one think what I dont understand is how to change states from script or how to do something like this: Iam hitting AI and I need to switch to Got Hit and then when hp is 0 switch to Dead state. Oh, and I see there is a trigger with name Low Health - in which script is this health defined?
    I feel stupid:) Thank you for answer.
     
  29. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Any chance for a wander or meander behavior somewhere?
     
  30. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Matis,

    The health is kept track of in the main AI script. As for the Got Hit state, that one is one of the states that needs a little bit of scripting (about one specific line). Take a look at the scene "10_state_got_hit", click on the AI avatar and it'll have a component named "ExampleGotHitComponent". In the OnTriggerEnter method you'll see the line that says, fsm.GotHit(projectileDamage); That is how the got hit state is switched to.

    So, obviously this is one of the states that isn't super easy to set up, but still isn't too painful.

    Hope that gets you in the right direction,
    Nathan
     
    Last edited: Nov 4, 2012
  31. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    No, there isn't but now that you mention it, would probably be a nice small addition to the behaviors.
     
  32. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    Hi,
    I notice that but dont work for me or most likely I do something wrong. I have enabled Got Hit state and assigned sound to it and Idle is my base state. My weapon code is:
    Code (csharp):
    1. void OnTriggerEnter (Collider other){
    2.     if(other.collider.tag == "enemy"){
    3.     var enemy = other.gameObject.GetComponent <ExampleAttackComponent>();
    4.     fsm.GotHit(enemy.HP -= Random.Range (player.minDmg, player.maxDmg));
    5.         }
    6.     }
    Sound is not played when I hit enemy but hp is lower. In ExampleAttackComponent I have coded enemy HP, level, attack,...
     
  33. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Matis,

    Here are a couple of thing to maybe do to make sure everything is correct.

    1) Try and place a debug statement at the bottom of the AIBehaviors.PlaySound method and if it comes up you're sound is "playing" but may not be heard for whatever reason.
    2) Make sure you have an audio clip attached to the GotHit state itself.

    If neither of these work and you're still having trouble and if you are comfortable, feel free to email your project to us and we'll take a look at it.

    Hope we can help you sort this out,
    Nathan
     
  34. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    Hi,
    never mind:) Iam testing my character on your AI in example scene and it is work so I have problem with my settings somewhere.

    EDIT: When I have working sound on your AI from example scene everything is ok BUT when I delete NavMesh AI setup and replace it with No NavMesh setup and set every thing exactly same sound is not play BUT is play when it is set back to NavMesh:)

    EDIT2: NathanWarden email sended
     
    Last edited: Nov 6, 2012
  35. theLittleSettler

    theLittleSettler

    Joined:
    Jul 14, 2012
    Posts:
    36
    After some time using this, I got the following error. I got this only when I build - I could play and all and had no trouble at all with it.

    Assets/AI/AIBehavior/Plugins/AIBehaviorsMadeEasy/EditorHelpers/AIBehaviorsStyles.cs(4,34): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?

    So I looked into it - apparently you need to have any scripts that use UnityEditor in an Editor folder. So not a problem, I move that script - and all the other scripts that use UnityEditor (there's several in the EditorHelpers folder). Worked like a charm...but some time later, Unity can't find those scripts anymore. So now I'm stuck (if I move it back, I get the first error).

    Ahh...Turns out AIBehaviorsStyles.cs needs #if UNITY_EDITOR
     
  36. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    @NathanWarden Did you receive my mail?
     
  37. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    That's right, it is written in such a way that the cs file needs editor code that needs to be wrapped in compiler flags. This is done for easy extensibility. The best way to avoid any confusion is to duplicate the Skeleton state or trigger and use that and rename it to what you want.
     
  38. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Matis, I did get your email and ran through your steps a few days ago to try and reproduce the issue, but it played sound with both the internal pathfinding and the Astar Pathfinding Project. I was going to try it again today or tomorrow to see if maybe I missed something or misunderstood your instructions.
     
  39. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    just update of my problem: For some reason today I get this error when I was hit the enemy:
    NullReferenceException: Object reference not set to an instance of an object
    AIBehaviors.GotHit (Single damage) (at Assets/Plugins/AIBehaviorsMadeEasy/AIBehaviors.cs:221)

    it is refer me to this line: navMeshAgent.destination = transform.position;
    when I delete this line sound is working but with this line no.
    As you know Iam using Astar Pathfinding.
    Any clue?
     
  40. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Matis,

    If you are using the default settings with Unity, what you can do is double click on the first NullReference error. It should open up MonoDevelop. Once it's open, it should have a line number on the left. Please let me know what that line number is and the name of the script that it was in.

    Thanks,
    Nathan
     
  41. Intrawebs

    Intrawebs

    Joined:
    Mar 24, 2011
    Posts:
    45
    Whats your Unity4 plans and specifically getting your scripts to work with custom animations made with the Avatar system and Mecanim?
     
  42. matis1989

    matis1989

    Joined:
    Mar 23, 2011
    Posts:
    162
    as I said it is AIBehaviors.cs line 221 which is: navMeshAgent.destination = transform.position;
     
  43. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Matis, sorry about that, I don't know how I missed that when it was right in front of my face. You should be able to delete that line of code. This will also be a fix in the next update. The original purpose of this line was to get the AI to stop when it's hit, but you should be able to set its' movement speed on the GotHit state to 0 if you still want this behavior.

    Hope this helps,
    Nathan
     
  44. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Intrawebs,

    If we decide to do this we definitely want to find the best way to implement a script that you can either use for Macanim or can use as a basis to implement your own. Some cases with Mecanim may be far too specific to use our system directly, so whatever we do impleent will more than likely be decoupled from the core system and simply be a script that you add to your AI character as a starting point or point of reference.

    Thanks,
    Nathan
     
  45. Intrawebs

    Intrawebs

    Joined:
    Mar 24, 2011
    Posts:
    45
    Thanks, I have a question. Im trying to come up with a simple behavior/state loop for an AI shooting at me. I have the following...

    idle...if player in view then ranged attack
    ranged attack...if not in sight then seek
    seek....seek player, when reached transition then ranged attack

    Lets say for example I'm on top of a freeway overpass, and the AI is below me at an angle. With this setup the AI would sit there looping and confused...I may be in view but not line of sight. Or am I not understanding sight and view correctly and what kind of state change do you recommend for this.

    Thanks!
     
  46. Zozo2099

    Zozo2099

    Joined:
    Jul 15, 2012
    Posts:
    478
    I have a question please to all, is the latest version works fine with Unity 4 final released recently?
     
  47. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Hi,

    I just downloaded Unity 4 and am using the pro trial. I baked a navmesh and used the default AI Behaviors setup on my AI Character (a spider-like creature). I purposely left off all colliders, so only the navmesh agent is attached. I have the AI switch from idle to follow when the player is in the line of sight. This works well, but when the AI reaches the player, the state doesn't change. I have it set to trigger at a range of 5. I've tried both melee and just having the AI go back to idle when within range. Neither works. The AI just stays in the follow state and presses up against the player.

    Similarly, I have trouble getting the AI to switch to patrol. I have a patrol group set up as in the instructions, but the triggers don't seem to work. I have idle as the initial state. Then I have the AI switch to patrol after 15 seconds. I also have the AI set to switch to patrol when the player is out of range. Neither works. The AI just stops moving when the player is out of range. I've tried playing with the distance at which the AI turns to the next patrol point, but it doesn't seem to make a difference. It could be a problem with the navmesh, but my terrain is pretty straightforward, no complex structures, mostly just slightly hilly standard terrain using the Unity terrain building tools. I wonder if you guys can think of something I might have missed. Thanks.
     
  48. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Intrawebs, the In Player Sight uses the main camera to determine if the AI is in the camera view. The line of sight is whether or not the AI can see the player.

    For the overpass example, you'll need to do one of two things depending on the circumstance.

    1) If the angle isn't too steep, you can increase the view angle of the AI to be able to see the player.
    2) If the angle is really sharp between the AI and the player (such as the AI is practically just above the player) you may need to right a custom state since this is kind of a special case scenario.

    Hope this helps,
    Nathan
     
  49. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Zozo, the latest version should work just fine with Unity 4.
     
  50. NathanWarden

    NathanWarden

    Joined:
    Oct 4, 2005
    Posts:
    663
    Hi Tool,

    My best guess is that you possibly have a collider on your main player, and that whatever the layer that your player is on is included in the Obstruction layers on the AI. So, if you main player controller is on the Player layer, you need to make sure that it's unchecked on the AI's "Visual Obstruction Layers" which is immediately under the "General AI properties".

    Hope this helps,
    Nathan