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 camtronius · May 25, 2014 at 09:51 PM · movementaidistanceienumeratorrts

focusing on a unit during unit detection in an RTS

Hey guys, I have an enemy unit which detects a player unit within 10 distance and then goes to its position. The problem is, when there are other units within 10 units distance, the enemy unit switches back and forth between the units. Is there a way I can make my enemy unit focus on the position of a single controlled player unit instead of all of them? I want to eventuall make it so the enemy chooses the nearest unit. Thanks, Code is posted here:

 IEnumerator distanceBoolToggle(){ //included in the coroutine to save memory
 
         while (numeratorBool==false) {
 
                 for (int i=0; i<UnitsList.Count; i++) { //this iterates through my list of player-unit objects
             
                                         float distance = Vector3.Distance (transform.position, UnitsList [i].transform.position);
                                         //this calculates the distance from one player-unit to enemy unit
             
                                         
                                         if (distance <= 10) { //if the distance is less than 10 between player unit and enemy unit
                                                 
                                                 GameObject playerObject = UnitsList [i]; //creates an object for that specific player unit
                                 
                                                 AstarAI playerObjectDetection = playerObject.GetComponent<AstarAI> (); //gets astarai code for that unit
 
                                                 targetPosition = playerObjectDetection.transform.position; //sets the enemy position to the pos of the player unit
 
                                                     float distanceToTargetPos = Vector3.Distance (transform.position, targetPosition); //finds distance between the two units
 
                                                         if (distanceToTargetPos > 3) {
 
                                                                 seeker.StartPath (transform.position, targetPosition, OnPathComplete); //if distance is more than 3 than travel towards its destination
                                                                 
                                                         }
 
                                                 }else{
                                         }
 
                                 }
                         
                     yield return new WaitForSeconds (1f);
                 }
         }        
     
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 Kiwasi · May 26, 2014 at 12:11 AM 0
Share

For readability put comments on the line before the code they refer to.

1 Reply

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

Answer by Kiwasi · May 26, 2014 at 12:25 AM

I would suggest only going through the list if there is no current target.

Something like this (meta code only)

  1. Define a private GameObject currentTarget

  2. If currentTarget = null or currentTarget distance > ten

  3. Run your code to find and assign a new currentTarget

An alternative is to use a trigger and create a private list on the enemy that grabs each player unit as it comes into range and deletes it as it leave range. This could make your script much cheaper to run as it only has to run through the units that are close, rather than every player unit on the map.

I'd also suggest using Vector3.sqrMagnitude instead of Vector3.Distance. Much faster for straight comparisons as there is no square root involved.

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

21 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

Related Questions

Help Calculating Shortest Total Distance Travelled By a Combination of Objects 0 Answers

How to forbid an NPC to move diagonally using Character Controller? 1 Answer

swipe distance detection dont work 2 Answers

Game NPC "Guide" Movement (Much like Navi from Zelda) 1 Answer

Help with 2D AI scripting 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