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

ICode (AI For Mecanim 2.0)

Discussion in 'Assets and Asset Store' started by DevionGames, Dec 18, 2013.

  1. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I do not realy understand what you mean. It is much easier to check your AIController asset, so i can see what you changed and what is not working. Very sorry that you need to upload the controller to get help/support, i will see if i can serialize the AIController as a string for one of the next updates, so you can post just the string and i can rebuild the controller from it.
     
  2. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    That's a good idea, I'm just so greatful for the help. If you could look at my controller and help with what I've done wrong, I'll be ETERNALLY greatful: https://drive.google.com/file/d/0BzKneB_P61RKUFVQZnhyNDRoLUU/edit?usp=sharing
    Thanks again man, Seriously. Your support is the best I've seen on the asset store.
     
  3. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
  4. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    I have bought when it was on sale. That's OK. I just wanted to give positive review.

    One thing that confused me is difference between "Update" and "State Action" to better understand which statement should go where. Also if you have time can you tell how to check for Player health. I want enemy to patrol state after player health reaches 0 (Dead).

    Thanks
     
  5. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Well, the diffrence is "State Action" runs in a Couroutine, maybe a better title would be IEnumurator. Update runs like the word say in Update.

    So for example if you set the speed of your AnimatorController in StateAction it is not smooth, but if you put it in the Update there is a damp time and it will transition more smooth, in the future i hope to remove one of them and handle it internal.

    Well in theory you can create an empty AIController for the player just by adding a health attribute, then you could use target information in your enemy AIController to get the health of your player.
     
  6. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    I have already tried this way adding AIController to the player and add a health attribute. The problem I am facing is since it adds a navmesh component, the player movement is effected and is not moving just animating in one spot. Do you have any clue. Thanks
     
  7. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    If you are more familiar with the system you can just remove the line in the AIRuntimeController.cs at the top, where it says [RequireComponent (typeof (NavMeshAgent))] and remove the component from your player. However add this component to your enemies.

    In the next update i am going to seperate the attributes and make an extra container script for this, this comes me also approach because i want to use this system in my rpg kit.
     
    Last edited: Jan 24, 2014
  8. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    I can do that. Thanks
     
  9. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    I tried and still its doing the same thing. So NavMeshAgent is not the problem. Let me know if you have any other ideas. Thanks
     
  10. Deepestblue

    Deepestblue

    Joined:
    May 11, 2009
    Posts:
    48
    Hi there,

    this asset looks really great so far. I wondered if it is possible to combine/run it with A* instead of NavMeshes. Could you please advise if this is possible without half a re-code? :)

    Thanks!
     
  11. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    He attacks the player and affects the health, but still isn't getting hit from the player. The sword damage sender script you wrote is on the player, and the damage reciever is on the enemy, I don't understand why he isn't getting hit.
     
  12. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I am working on a solution for custom actions without changing internal code. I think i still need 1-2 days for this.

    At the moment it does not support external pathfinding out of the box. However this is already on my todo list.


    You only need the SwordAttackDamageSender.cs to attach on the enemy, but as i said you need to trigger the SwordAttack() method.
    To trigger this method you can use the default unity event system, or call it in a diffrent way (like checking if the player presses a button...)
     
  13. Deepestblue

    Deepestblue

    Joined:
    May 11, 2009
    Posts:
    48
    Great, thank you. As soon as it supports A* it is in all my projects. :)
     
  14. milktoast

    milktoast

    Joined:
    Jan 29, 2014
    Posts:
    3
    Zerano I think this tool is great and has allowed me to almost fully create the AI for my game but I have found a bug, my game requires static objects to have the navmesh obstacle with the carve function turned on. The idea is that if a player chooses to destroy that object to gain its resources hey open a path for the AI to travel. Anyway if the carve function is turned on the AI will not move. Is this something you have seen or is there a setting I'm not using.
     
  15. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    The current sword attack button on the player is a public bool:
    Code (csharp):
    1. if (Input.GetButton("Attack1"))
    2.             {
    3.                 animator.SetBool("lightAttack", true);
    4.             }
    How would I implement SwordAttack()?
    Would it also have be identified at the top as a public class?
    Sorry for this question ^^
     
  16. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Great feature this carving , i did not know about it at all, but i just tested it and it seems to work with the ai. I forgot to rebacke the navmesh when i turned the navigation static off and moved my test cube with the NavMeshObstacle component on it the first time. The ai was of course not able to move over it , but after rebake all worked fine. Maybe you did the same mistake?

    No you need to add an event on your animation and add "SwordAttack" as function in the parameter field.
    I just added an example with a third person character and a sword animation, so you will see it in the next update.
     
    Last edited: Jan 29, 2014
  17. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    An event? To which animation the enemy or the character? Sorry about this.
     
  18. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    To the character when you swing the sword it should trigger the SwordAttack method.

    Select your player model, go into the animation tab, choose the sword attack animation, scroll down to the "Events" and add a new one with the function parameter "SwordAttack" at the timeline when you want to make damage to the enemy.
     
  19. milktoast

    milktoast

    Joined:
    Jan 29, 2014
    Posts:
    3
    Thanks for the quick response. Yeah i tried what you did and they still won't move I tried it with the off mesh link system and got the same results if i turn it off then start the game the ai begin to move and if i turn the off mesh link or carve tool on the ai that has already begun to move keep moving but the others that are waiting to generate the path stand still and never move.
     
  20. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I've still not got it to work. Would I be able to upload my project so you can take a look at what I might be doing wrong? So sorry about this :(
     
  21. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Yes sure.
     
  22. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I'll upload now and PM you a link. Sorry again.
     
  23. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I am currently uploading a new version to my store with a support for this pathfinding solution http://arongranberg.com/astar/
    Please follow instructions in the AStar Support.pdf to get the example scene to work. The unity asset store will also get this update after they accept the last update, sorry for this.

    Demo

    $Bot.png

    Okay great, no problem at all.
     
    Last edited: Jan 30, 2014
  24. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Okay it is now possible to create custom conditions. Please check how to create custom action/condition in the updated documentation.

    To get started you can create a player health holder script.

    PlayerHealthController.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerHealthController : MonoBehaviour {
    5.     public int health = 100;
    6. }
    And a condition similiar to this one. Please note comparerType and value are serialized so you need to create a new asset if you want to change them without modifing the old values/asset.

    PlayerHealthCondition.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class PlayerHealthCondition : CustomCondition {
    6.     //Reference to the PlayerHealthController attached to the player game object.
    7.     private PlayerHealthController playerHealthController;
    8.     public ComparerType comparerType;
    9.     public float value;
    10.  
    11.     public override bool Validate (AIRuntimeController controller)
    12.     {
    13.         //Check if the ai has a target
    14.         if (controller.target == null) {
    15.             //Target is null so return false
    16.             return false;
    17.         }
    18.    
    19.         if (playerHealthController == null ){
    20.             //Get a reference to the PlayerHealthController
    21.             playerHealthController = controller.target.GetComponent<PlayerHealthController> ();
    22.         }
    23.  
    24.         switch (comparerType) {
    25.         case ComparerType.Greater:
    26.             //Check if the health is greater then the defined value
    27.             return playerHealthController.health > value;
    28.         case ComparerType.Less:
    29.             //Check if the health is less then the defined value
    30.             return playerHealthController.health < value;
    31.         }
    32.  
    33.         return false;
    34.     }
    35. }
    Your asset will look like this:
     
    Last edited: Jan 30, 2014
  25. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I've PM'd you the link now, If you could just to ease sake, would you be able to reply on here? Or screenshot how I've gone wrong / how to fix this? I feel so bad but I'm very greatful for your help and support. I've easily pay £££ a month for support like yours!
     
  26. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    Looking forward to Astar support hitting the asset store. I know its a bit ornery to work with but it is way more flexible than Unity's pathfinding.
     
  27. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Okay the problem was that your enemy character had no collider on the root transform and so the raycast was never successfull. Besides that i would also add a LayerMask to the Raycast and remove the player layer from it, so you do not hit the player but only enemy.
     
  28. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    One final question: How do I add a LayerMask to the Raycast? And I assume a capsule collider would be fine for root transform, right?
     
  29. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SwordAttackDamageSender : MonoBehaviour {
    5.  
    6.     public int damage;
    7.     public float maxDistance;
    8.     public string healthAttribute;
    9.     public LayerMask mask;
    10.     private void SwordAttack(){
    11.         RaycastHit hit;
    12.         if(Physics.Raycast(transform.position+Vector3.up,transform.forward,out hit,maxDistance,mask))
    13.         {
    14.             Debug.Log(hit.transform.name);
    15.             AIRuntimeController behaviour= hit.transform.GetComponent<AIRuntimeController>();
    16.             if(behaviour)
    17.             {
    18.                 BaseAttribute defenderAttribute= behaviour.GetAttribute(healthAttribute);
    19.                 if(defenderAttribute != null)
    20.                 {
    21.                     Debug.Log("Consume");
    22.                     defenderAttribute.Consume(damage);
    23.                 }
    24.             }
    25.         }
    26.     }
    27. }
    28.  
     
  30. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I've updated the SwordAttackSender, I've put the Enemy in the Enemy Tag, With the script in Inspector selected enemy, put the enemy in a capsule collider. The player now bumps against the enemy but the enemy still doesn't take damage. I don't know what I've left out. Is it possible to create a small picture walkthrough or a video walkthrough of how you did it? I know this is a lot to ask, I just don't understand why I'm doing what you're saying and it's not working :(
     
  31. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    Zerano,

    I am confused. I know the "PlayerHealthController.cs" script go on the Player. How about the "PlayerHealthCondition.cs" script where this script will go.
     
  32. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I created a GUI texture health system that looks for Health - or Health + from objects. With DamageReciever.Cs, I attached this to the enemy instead of the player and linked the health I created. It seemed to work through that.
    Code (csharp):
    1.     public Health health;
    2.  
    3.     private void OnGetHit(AttackState state){
    4.         health.ModifyHealth(-3);
    5.     }
    6. }
    I'll PM you my Health code, in case you wanted to use it.

    EDIT: Sent the code, should work. Shouldn't be any problems but if they're are. Let me know.
     
    Last edited: Jan 31, 2014
  33. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140
    I have put "PlayerHealthCondition.cs" on the enemy. First I have dragged AIRuntimeController on to the enemy and then assigned the "PlayerHealthCondition.cs" to that controller and then set the ComparerType to less and value to 1. What happened when it hit the script "PlayerHealthCondition.cs" the ComparerType is coming out greater and value 0 which I believe is the default value. I don't know what I am doing wrong ?
     
  34. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    I sent you a private message with scripts that I used to get it to work. By your name on the top of this page where it says "Welcome, Kotor" to the right should be Notifications and Inbox. If you check that out and have a read, it should fix your problem. It might not, but it worked for me.
     
  35. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Yes, in the next video i will extend the Attack AIController. If you will still have problems i can change your project and send it back to you.

    Please follow this tutorial.
     
  36. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    If you could alter the project, I'd be VERY appreciative!
     
  37. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I also see some other problems in your project, if you want you can contact me in private via skype and we can talk about it and i can do some suggestions.
     
  38. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    Sure! That'd be great! Would you be able to PM your Skype name?
     
  39. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Done.

    Here is a new tutorial.
     
  40. kotor

    kotor

    Joined:
    Dec 3, 2013
    Posts:
    140

    Thank you. Now it worked perfect. I misunderstood.
     
  41. PixelFrog

    PixelFrog

    Joined:
    Dec 2, 2012
    Posts:
    8
    Hi,
    Your asset looks awsome :) Does it require Unity Pro?
     
  42. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    Thank you, it will work with unity free, but the ik actions would require unity pro.
     
  43. thedreamer

    thedreamer

    Joined:
    May 13, 2013
    Posts:
    226
    Can I use this Mecanim AI System to make RTS GAME? I control the unit But sometimes the unit itself acts by AI
    And unit can battle sometimes collect, Training....
    I am trying to create a mobile game Performance is also an important issue.
    The Mecanim AI is good choice for RTS GAME?
     
    Last edited: Feb 3, 2014
  44. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I would say it is possible, but would require some custom conditions. I would love to make an rts demo, but i do not think it will happen in the next days, since i still need to finish a new showcase demo.
     
  45. thedreamer

    thedreamer

    Joined:
    May 13, 2013
    Posts:
    226
    WOW.. Would you make RTS demo? some day~~

    RTS Demo that will be helpful to many people As well as Mecanim AI system

    Because RTS Demo shows The great thing about this product

    I have another question .. is this a system that can replace the FSM ?

    I wonder this AI System can use to make farming or citybuilder..

    I am attracted to the Mecanim AI system instead of FSM

    how do you think player caracter use this AI System ? not enemy..
     
    Last edited: Feb 3, 2014
  46. Apiweb

    Apiweb

    Joined:
    Nov 26, 2011
    Posts:
    57
    I'm with 3 projects:
    • 2D Sidescroller Game (Donkey Kong, Mario World style, etc...)
    • Realtime Strategy (Age of Empires style)
    • Turn Base Game (Xcom: Enemy Unknown, Shadowrun style)

    The Mecanim AI System, can handle these different types of games?
    In some of these projects will not have the models animations, even so the system would work only for calculating the AI ​​of enemies and NPCs?
     
  47. 99thmonkey

    99thmonkey

    Joined:
    Aug 10, 2012
    Posts:
    525
    Questions for you:
    1 - Does this work for flying or swimming AI?
    2 - Is it easy to combine multiple attack styles such as Melee if in Melee Range and Ranged Attack if in Attack Range but not Melee Range
    3 - UFPS Demo - when I jump across an area that is only a small ways away, the AI runs around trying to get to me instead of just shooting at me across the gap. Is it easy to make this work properly?
    4 - In addition to above, if I have an AI on a cliff and I'm on the ground far below, can I set the Ranged Attack for a far distance to shoot like a sniper? If yes, does it also do line of sight test.
    5 - Is it possible to have different speeds - like walk wander and run chase?
    6 - What are all the AI states? Idle, Chase, Get Help, ...?
     
  48. RyuuzakiBjorn

    RyuuzakiBjorn

    Joined:
    Jan 21, 2014
    Posts:
    25
    For anyone thinking of getting this asset but not sure... Just buy it. It's such a powerful asset and something that should come pre-installed with Unity. It's an essential asset that I definitely recommend that you don't pass up this crazy sale offer. I've personally not come across a more important asset for my projects. Seriously. Don't think about it. Buy it for this crazy price. Even if you're unsure you'll use it for your current project, because your future projects will thank-you for it. This is a must have asset and if you're reading this haven't bought it yet. BUY IT NOW. Seriously. BUY IT. One more time? BUY IT. NOW. I can't sing enough praise about the asset. You won't regret your purchase, the asset is well worth over £200. So what are you waiting for? Buying the asset will make your life so much easier. I've got a video coming up in the next few days on what the asset has done for my project, But I don't know how long the asset is on sale for. All I can say is that you need this asset in your Unity life. Forget all the other AI assets out there, I've tried a lot of them and this knocks the seven shades of poop out of them. THIS IS AN ESSENTIAL ASSET. GET IT NOW.
     
  49. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    Even though Playmaker is not supported, can it be used the same game with Mecamin AI? My son would love this for his games but he uses Playmaker.
     
  50. DevionGames

    DevionGames

    Joined:
    Feb 22, 2010
    Posts:
    1,624
    I reused some code in my rpg kit for the new item system, however i do not know how you would use it for the player. Well i am open for suggestions and in the future i will create many prototypes with this system, this means also that some things may change or added. For the new demo i also added a new action that will come with the next updates, it is nothing big but i noticed it is very usefull(DestroyComponent).

    In theory it should work pretty well, however each project is diffrent and would need some extensions or modifications.

    1. At the moment the movement is handled on the navmesh, but this is a great idea to extend the ai system.
    2. Yes it is simple to combine those things. What i have done in the Ranged Attack example is simply i check the distance -> 8 > x Ranged Attack, 8 < x Meele, you can even create diffrent ranged attacks or diffrent meele attacks.
    3. I will check it.
    4. Yes this is possible with unity pro using ik. LineOfSight can be used with View condition, maybe i should rename it sorry.
    5. Yes fully possible creating diffrent states. You can even have diffrent speeds for one of those. For example wander can have diffrent speeds...
    6. Idle, Chase is all possible, but GetHelp is currently not integrated out of the box, but i am going to create an example on how you can achieve this.

    Thank you very much!

    I do not see why you would not be able to use both in one game. Maybe there will be some naming problems, because i have not integrated Namespaces yet and if Playmaker uses the same name for a script, there could be some errors. But namespaces are on my todo list already.