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 ImpactUK · Dec 13, 2012 at 07:48 PM · transformdistancelookatclosest

Distance from multiple enemies but follow master

I've made a dog that follows its master through my game. I managed to get it to attack ONE of the enemies on the game if it was within a certain distance. This would be cancelled, though, if its master ran away from the enemy(it would follow its master instead of attacking). I used this:

     /*var enemies = GameObject.FindGameObjectsWithTag("Enemy").transform;
     
     var distanceFromMaster = Vector3.Distance(master.position, transform.position);
     var distanceFromEnemy = Vector3.Distance(enemies.position, transform.position);
 
 
         if (distanceFromEnemy < 12 && distanceFromMaster < 15) {
             transform.LookAt(enemies);
             transform.Translate(Vector3.forward * speed * Time.deltaTime);
         }
         else {
                 if (distanceFromMaster > 3) {
                     transform.LookAt(master);
                     transform.Translate(Vector3.forward*speed*Time.deltaTime);
                 }
         }
             
 
 }

Now, instead of attacking just one enemy, i wanted to make it decide where the nearest enemy was out of a few enemies and attack that one. But i also want it to run after its master if the master runs away. The current one i'm using makes the dog loop looking at its master and the closest enemy, but i can't find what i need to change. Can someone help? Heres the one im trying to fiddle with now:

     var distanceFromMaster = Vector3.Distance(master.position, transform.position);
     if (distanceFromMaster > 3) {
         transform.LookAt(master);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
     else {
     
         var enemies: GameObject[];
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
         var closest : Transform;
         var distance = Mathf.Infinity;
         var position = transform.position;
         
         for(var enemy : GameObject in enemies) {
             var difference = (enemy.transform.position - position);
             var currentDist = difference.sqrMagnitude;
             
             if (currentDist < distance) {
                 closest = enemy.transform;
                 distance = currentDist;
             }
         }
         
         transform.LookAt(closest);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
     
 }
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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

transform.LookAt issue 1 Answer

How to prevent Z axis rotation ? 1 Answer

LookAt transform plus a y value 1 Answer

Using legacy particles and transform.LookAt() 0 Answers

Get LookAt vector of a tranform 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