Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Supershandy · Apr 23, 2013 at 06:56 PM · javascriptraycastai

AI Evasion help

Hi there, I'm having a little trouble with my avoidance script, I have a raysphere cast set up to detect if an enemy fighter is too close and then a random position is generated for it to fly towards and then after waiting 4 seconds it checks to see if the enemy is still too close otherwise it turns back and attacks, however they seem to be constantly flying around each other.

Debug.Log shows that they are doing the job right, but i've increased the random distance from 40 up to 500 and nothing changed. I have a feeling that i've missed something obvious or forgotten to add something important, but here's the code if anyone has a clue

 if (Physics.SphereCast (EvadePosition.position, 20, EvadePosition.forward, Hit, 20))
     {
         if(Hit.transform.tag == "Enemy")
         {
             IsAttacking = false;
             IsEvading = true;
             transform.Translate (0, 0, EvadeSpeed * Time.deltaTime);
             Evade();
         }
     }
 
 function Evade ()
 {
     var EvadePoint = Random.insideUnitSphere * 500;
     var rotation = transform.rotation;
     var position = transform.InverseTransformPoint(EvadePoint);
     var direction : float = position.x > 0 ? 1.0 : - 1.0;
     var angle = Vector3.Angle (Vector3.forward, position) * direction;;
     var targetDirection = (EvadePoint - transform.position).normalized;
     var lookRotation = Quaternion.LookRotation (targetDirection);
     transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
     transform.Rotate (0,0,-angle * Roll);
     
     yield WaitForSeconds (4);
     
         IsAttacking = true;
         //Debug.Log("Resuming Attack - Blue Fighter");
         IsEvading = false;
         transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
         Attack();
     
                 
             
 }
Comment
Add comment
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

1 Reply

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

Answer by AlucardJay · Apr 23, 2013 at 08:38 PM

I think the problem is you are not checking if it is evading already before calling Evade() again.

 if(Hit.transform.tag == "Enemy" && IsAttacking)

This should stop everything under that line from recalculating if you are already evading

 if (Physics.SphereCast (EvadePosition.position, 20, EvadePosition.forward, Hit, 20))
     {
         if(Hit.transform.tag == "Enemy" && IsAttacking)
         {
             IsAttacking = false;
             IsEvading = true;
             transform.Translate (0, 0, EvadeSpeed * Time.deltaTime);
             Evade();
         }
     }
 
 function Evade ()
 {
     var EvadePoint = Random.insideUnitSphere * 500;
     var rotation = transform.rotation;
     var position = transform.InverseTransformPoint(EvadePoint);
     var direction : float = position.x > 0 ? 1.0 : - 1.0;
     var angle = Vector3.Angle (Vector3.forward, position) * direction;;
     var targetDirection = (EvadePoint - transform.position).normalized;
     var lookRotation = Quaternion.LookRotation (targetDirection);
     transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
     transform.Rotate (0,0,-angle * Roll);
     
     yield WaitForSeconds (4);
     
         IsAttacking = true;
         //Debug.Log("Resuming Attack - Blue Fighter");
         IsEvading = false;
         transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
         Attack();    
 }
Comment
Add comment · Show 6 · 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
avatar image Supershandy · Apr 24, 2013 at 12:47 PM 0
Share

Thanks, i'll try it when i get home, though what is the "IsAttacking" doing in the if statement brackets? Is it just checking to see if the variable statement is true or false and acting accordingly?

avatar image AlucardJay · Apr 24, 2013 at 12:59 PM 0
Share

That's the theory.

  • enemy is attacking

  • Spherecast to check if should be evading

  • check the tag and if is attacking

  • then go to evading behaviour

next update

  • enemy is evading

  • Spherecast to check if should be evading

  • check the tag and if is attacking

  • is already evading, so no need to apply evading behaviour

  • once the yield has completed, goes back to attacking

avatar image Supershandy · Apr 24, 2013 at 01:24 PM 0
Share

Ah, slight problem, the "IsAttacking" is a function of the ai when it is attacking something, not if it is being attacked, although the evade function is also called if it is being damaged by enemy fire, the spherecast is just there to check if it is too close to the enemy (suppose it could also be use for obstacle avoidance).

Should the if statement in that case checking to see if "IsEvading" is set to false if it is too close to the enemy?

avatar image AlucardJay · Apr 24, 2013 at 02:35 PM 0
Share

Oops, yeah use not isEvading. It's hard to tell what happens before the spherecast.

 if(Hit.transform.tag == "Enemy" && !IsEvading)
avatar image Supershandy · Apr 24, 2013 at 02:52 PM 0
Share

I probably should have explained it a little better but i'll let you know how it turns out once i get back in a couple of hours. Thanks again

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AI Shooting through walls. 1 Answer

Functions being ignored 2 Answers

A node in a childnode? 1 Answer

When Object Pooling Bullet Decal Arrays in UnityScript, My code use the original prefabs instead of the instantiated cloned prefabs 2 Answers

FPS PISTOL 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