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 Drandoss · Feb 19, 2011 at 07:50 PM · arraytargeting

on keypress find closest target, then change its tag?

Ive been working on a space rts type game, basicly im trying too get the closest transform, wich ive done from looking at awnsers here, and the code works for that well at this point. the Array returns the closest targets Transform just fine when the FindManual(enemiesTargs) is called on KeyCode.Tab, heres where Im not sure and need help. I dont get any errors wit this at all, however when I go too hit tab, All the targets on the screen get changed? Ive tryed about 10 diffrent methods too do this all resulting in ALL the targets getting there name changed, Im guessing its something too do with the way Im instancing my Enemys? they all are enemeys(Clone) and all start with Tag "none". Or target_data keeps passing by All the arrayed objects when I call it on the keypress?.

I need a way too do this, and only change the Tag of the closest enemy returned when tab is pressed.

var target_data : Transform; var myPosition : Vector3; var enemiesTargs : Transform[];

function changeClose(){ //Each enemey spawned has this sensor, if its changeTag is true it changes its own tag too "player1_target" target_data.GetComponent("mouse_sensor").changeTag = true; //change closest targets tag? }

function FixedUpdate() { if(Input.GetKey(KeyCode.Tab)) //Tab pressed, Find closest, load target_data? GetManual(); target_data = FindManual(enemiesTargs); changeClose(); //where I change the tag of the closest? }

function Update() { myPosition = this.transform.position; //This is the players position too check agianst. }

function GetManual () { var enemyObjects = GameObject.FindGameObjectsWithTag("none"); enemiesTargs = new Transform[enemyObjects.Length]; for (i = 0; i < enemyObjects.Length; i++) { enemiesTargs[i] = enemyObjects[i].transform;

 }

}

function FindManual (targets : Transform[]) : Transform { var closestDistance = (enemiesTargs[0].position - myPosition).sqrMagnitude; var targetNumber = 0; for (i = 1; i < targets.Length; i++) { var thisDistance = (enemiesTargs[i].position - myPosition).sqrMagnitude; if (thisDistance < closestDistance) { closestDistance = thisDistance; targetNumber = i; } } return enemiesTargs[targetNumber]; }

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 mgamall · Sep 12, 2011 at 02:42 AM 0
Share

any answer?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Sep 12, 2011 at 03:41 AM

If you just want to change the tag of the nearest enemy when Tab is pressed, I think this is the easier way to do that:

 function Update(){
   if (Input.GetKeyDown(KeyCode.Tab)){
     var enemies = GameObject.FindGameObjectsWithTag("none");
     var here: Vector3 = transform.position;
     var minDist: float = Mathf.Infinity; // init with max possible value
     var nearest: GameObject;
     for (var enemy: GameObject in enemies){
       var dist = Vector3.Distance(enemy.transform.position - here);
       if (dist < minDist){
         nearest = enemy;
         minDist = dist;
       }
     }
     if (nearest){
       nearest.GetComponent("mouse_sensor").changeTag = true;
     }
   }
 }


 
 
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

drawing GUI or Instantiate an object to highlight multiple object 2 Answers

Find Target From List 1 Answer

C# Targetting Help 1 Answer

Array.map Support 1 Answer

Why Does Initialising Built-In Array of Classes Require Constructor? 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