Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Demonicdaron · Aug 08, 2015 at 05:02 AM · aienemycombat

Enemy AI movement problem

I have been trying to understand this for a long while now, and still haven't come to a conclusion. I have an enemy AI working with two colliders (aggroRange and attackRange) that set two bools which indicate if the player is in either of the ranges. The two colliders are placed in two separate gameObjects which are both children of the enemy gameObject (where the enemy AI script is placed). The bools _playerIsInAggroRange and _playerIsInAttackRange are set to true on OnTriggerEnter and to false on OnTriggerExit by two separate scripts placed in each collider.

Here is a code map showing the connection between the bools and the functions:

alt text

The AI is pretty straight forward:

         //If player is within aggro range, but not attack range
         if (_playerInAggroRange)
         {
             if (_playerInAttackRange)
             {
                 if (_moving) stopMoveAnimation();
                 if (_attackFrequency <= 0 && !_moving)
                 {
                     startAttackAnimation();                //Attack
                     _attackFrequency = AttackRechargeTime; //Reset Timer
                 }
             }
             else
             {
                 moveTowardsPlayer();
             }
         }
         else
         {
             moveToSpawn();
         }
 /*This is inside the Update() method of the AI script, which controls everything about the enemy (movement, combat etc.)*/

An example of what the OnTriggerEnter/Exit functions do:

 void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player"))
         {
             Debug.Log("Player got inside attack");
             EnemyScript.PlayerIsInAttackRange(true);
         }
     }

For some reasons, the last else (that leads to moveToSpawn()) is called every frame, even if the first condition (_playerIsInAggroRange) is true. In fact, when I tried to debug the bool variables, they appeared to be true and false in the same frame. The only functions that write the bools are called by the OnTriggerExit/Enter functions in the aggroRange and attackRange collider gameObjects, and they are called properly (tested with debug.log), so I don't see how this is happening!

Please tell me if you need any other info to help me fix this problem, any help would be greatly appreciated!

kjhkhkh.png (17.8 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Aug 08, 2015 at 06:42 AM 0
Share

Where/how do you declare "EnemyScript"? And can you describe the hierarchy of gameobjects in more detail? (Particularly, which elements have rigidbodies?)

avatar image Demonicdaron · Aug 08, 2015 at 09:58 AM 0
Share

alt text

This is the hierarchy. The Enemy AI script is in the "Parasite" object, the other two scripts are in AttackRange object and AggroRange object respectively. There are no rigidbodies involved anywhere, only CharacterControllers used as colliders (movement is done through animation)

zxcv.png (3.1 kB)
avatar image getyour411 · Aug 08, 2015 at 02:11 PM 0
Share

"the last else (that leads to moveToSpawn()) is called every frame, even if the first condition (_playerIsInAggroRange) is true" -

No. The IF/ELSE construct is not a weak test, so if the first condition is true ELSE is not called that frame. Given your description, look for other places in your code where you reference/change this stuff.

$$anonymous$$onobehavior find references might help.

avatar image Demonicdaron · Aug 08, 2015 at 05:24 PM 0
Share

I've been printing a debug log from that condition tree, and it prints both whats under the IF and whats under ELS$$anonymous$$ So logic says, there is either some change in the variable before the ELSE is called, or the Debugo.Log message came from two separate frame (consecutive probably, as the number of messages was growing constantly). But, unfortunately, there are no other references to those variables (I've even shown evidence of that on the Code $$anonymous$$ap at the top of the post), so I have no idea why this happens

avatar image sparkzbarca · Aug 09, 2015 at 03:52 AM 1
Share

Do you have multiple parasites cause if so the

getting true and false could just be because one is calling move to spawn and the other isn't.

the other possibility is your move towards player script is wonky and moving the parasite in the opposite direction and so the moment it "enters" it goes ok im in aggro range, ok i'm not in attack yet, ok move to player, mvoes the oppostie direction on accident. Ok i'm out of aggro range move to spawn.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Demonicdaron · Aug 09, 2015 at 04:11 PM

"Do you have multiple parasites cause if so the

getting true and false could just be because one is calling move to spawn and the other isn't.

the other possibility is your move towards player script is wonky and moving the parasite in the opposite direction and so the moment it "enters" it goes ok im in aggro range, ok i'm not in attack yet, ok move to player, mvoes the oppostie direction on accident. Ok i'm out of aggro range move to spawn." By sparkzbarca

You nailed it! The ranges scripts where referencing the same script because I was using a stupid public variable -.-', I've changed it to a private and retrieve it at start using GetComponentInParent, so each script will get a proper reference to its enemy script. Thank you, I was getting crazy and the game was getting laggy!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Enemy AI Not Working? 1 Answer

Checking name from hit from Raycast in array 2 Answers

Assigning a particle system to a variable 2 Answers

How do I make enemy AI with third person assets? 0 Answers

Kill character on impact 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges