Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by CubePhysics · Dec 31, 2015 at 03:11 PM · aidetection

struggling with enemy AI

My enemy dodges an incoming projectile by detecting if its aimed to its head (the top) or the bottom torso (the bottom). If it is heading to the top then the enemy moves down and if its heading to the bottom it moves upwards to dodge it, It works fine except the enemy is colliding with the ground or "ceiling" of the scene it cannot go any further and so it stays where it is and carries on getting shot. How can I overcome this and make the enemy move downwards if its stuck at the top and vice versa. Thanks

     if (GameObject.Find("Laser(Clone)") != null){
             float topProjectileDistance = Vector2.Distance(topHalf.transform.position, GameObject.Find("Laser(Clone)").transform.position); 
             float bottomProjectileDistance = Vector2.Distance(bottomHalf.transform.position, GameObject.Find("Laser(Clone)").transform.position); 
 
                 if(topProjectileDistance <= 10 && bottomProjectileDistance <= 10){
                 rb.AddForce(Vector3.right * speed/2 * Time.deltaTime);
             
                 if(topProjectileDistance < bottomProjectileDistance)
                 {
                     rb.AddForce(Vector3.down * speed * Time.deltaTime);
                 }else{
                     rb.AddForce(Vector3.up * speed * Time.deltaTime);
                 }
 
             }
         }
Comment
Add comment · Show 1
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 ShadyProductions · Dec 31, 2015 at 04:55 PM 0
Share

you can always try to disable the rigidbody when you move, and use the prefab's position ins$$anonymous$$d of addforce and then re-enable it after its done dodging. Just an idea.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by callen · Dec 31, 2015 at 05:15 PM

You want the enemy to move...

  • Down when its top half is shot at

  • Up when its bottom half is shot at

  • Down when it's at the top of the screen

  • Up when it's at the bottom of the screen

Currently, your code only handles the first two. You should modify your 2nd if-statement to include the other cases. Something like...

 public float avoidBoundsDistance = 5;
 ...
 // calc these the same way as top/bottomProjectileDistance
 float distanceToCeiling, distanceToFloor; 
 if(distanceToCeiling < avoidBoundsDistance)
   // add force down
 else if(distanceToFloor < avoidBoundsDistance)
   // add force up
 else if(topProjectileDistance < bottomProjectileDistance)
   // add force down
 else
   // add force up
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
avatar image
0

Answer by Elier · Jan 02, 2016 at 08:17 AM

It's not very complicated, you already have most of the work done:

First create a bool variable that will indicate if the enemy will dodge moving up or moving down (true = up, false = down).

Instead of applying the forces inmediatly, just set the variable using the calculations that you already have for the projectile targeting the head or torso.

Now, before trying to move up or down, check how close the enemy is to the floor or ceiling using Raycast. If the enemy is too close and can't dodge in the direccion it should, reverse the boolean.

Finally if the boolean is true apply the force upwards, or else apply the force downwards.

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

47 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Turret AI Script - one way detection 0 Answers

Rain AI follow path and detect 0 Answers

How to keep an array of targets in detection radius? 0 Answers

How would I Move a rigidbody towards another object? 3 Answers

Trying to get AI to follow who ever is closer 2 Answers


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