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 Player_1 · Jun 08, 2012 at 07:54 PM · spacetargetingtab

tageting system 3d space

Hi in my space game i want to have a tab to next enemy but i cans seem to get the tab to work on my script i have the list showing my 3 baddys but when i hit tab there is noting going on any help would be amazing here is the code i have so far for it.

 public class Targetting : MonoBehaviour {
 public List<Transform> targets;
 public Transform selectedTarget;
 
 private Transform myTransform;

 // Use this for initialization
 void Start()
 { 
     targets = new List<Transform>();
     selectedTarget = null;
     AddAllEnemies();
     myTransform = transform;
 }
 
 public void AddAllEnemies()
 {
     GameObject[] go = GameObject.FindGameObjectsWithTag("enemy");
     
     foreach(GameObject enemy in go)
         AddTarget(enemy.transform);
 }
 
 public void AddTarget(Transform enemy)
 {
     targets.Add(enemy);
 }
 
 private void SortTargetsByDistance()
 {
     targets.Sort(delegate(Transform t1, Transform t2) { 
         return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
             });
         
 }
 
 private void TargetEnemy()
 {
     if(selectedTarget = null)
     {
         SortTargetsByDistance();
         selectedTarget = targets[0];
     }
     
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetKeyDown(KeyCode.Tab))
     {
         TargetEnemy();
     }
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Berenger · Jun 08, 2012 at 08:53 PM

Your code seems to select the closest target properly. You should use sqrMagnitude instead of the magnitude btw, no need to calcul the sqrt for comparison.

However, you never do anything with that selection. Do you want to use LookAt on it ?

Comment
Add comment · Show 2 · 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 whydoidoit · Jun 08, 2012 at 08:59 PM 1
Share

I'd just add that if you are just selecting the closest it is much less work to do a single pass of the list and select the item with lowest distance, a sort will do much more work if you are only going to select the lowest value.

avatar image Berenger · Jun 10, 2012 at 11:50 PM 0
Share

Player_1 : i think my problem lies in the private void sortTargetsByDistance. I thought I set that up for a 3d environment, but it seems to only see things when I line the ship up with them at the same Y. What changes do I need to make so that it will see all the "enemy" tags in the world in the world? Also yes im going to use a LookAt function and create an orbit script for the space combat and docking.

$$anonymous$$e : As $$anonymous$$ike said, you don't actually need to sort the list. Initiate a float at $$anonymous$$athf.infinity, select the first, loop through all the transforms and calculate the sqr$$anonymous$$agnitude, if it's < than the previous distance, replace it and replace the selection. At the end end, you'll get the closest one.

avatar image
0

Answer by Player_1 · Jun 09, 2012 at 08:48 PM

i think my problem lies in the private void sortTargetsByDistance. I thought I set that up for a 3d environment, but it seems to only see things when I line the ship up with them at the same Y. What changes do I need to make so that it will see all the "enemy" tags in the world in the world? Also yes im going to use a LookAt function and create an orbit script for the space combat and docking.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

3D spaceshooter : targeting system 3 Answers

Can I create a segment that only includes new users? 0 Answers

Name for list item selection/movement bar? 0 Answers

Is it possible to rename tabs in editor? 2 Answers

How to make a Homeworld Style Camera. 3 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