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 KOCGI · Sep 14, 2018 at 11:45 PM · transformenemytracking

Track only closest enemy transform

How can I achive that I'm only targeting the closest enemy? Even better would be to "loose" the target if the target comes to close to the player. Jumping on the next target in range, if there is any.

Here is my code:

 public class PlayerInput : MonoBehaviour
 {
     public Transform target;
 
     void lookForTarget()
     {
         if (GameObject.FindWithTag("Enemy") != null)
         {
             target = GameObject.FindWithTag("Enemy").transform;
             target.Find("TargetGui").GetComponent<Renderer>().enabled = true;
         }
     }
 }
 

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nathanlink169 · Sep 15, 2018 at 04:48 PM

 Transform[] enemies = GetEnemies();
 
 float closestDistance = float.MaxValue;
 Transform closestEnemy = null;
 
 for(int i = 0; i < enemies.Length; ++i)
 {
     float currentDistance = Vector3.Distance(m_player.position, enemies[i].position);
     if(currentDistance < closestDistance)
     {
         closestDistance = currentDistance;
         closestDistance = enemies[i];
     }
 }

In the end, whatever is in closestEnemy will be the one you're looking for.

Comment
Add comment · Show 4 · 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 KOCGI · Sep 16, 2018 at 12:45 AM 0
Share

I still don't get it, trying desperately to integrate this, but I have no idea how. Where should I put it? This dosn't work, too- :( I have the example code from unity, but it's dosn't get the closest, running the loop means no difference than not running it. In other words, the targets won't switch. Please show full code

avatar image nathanlink169 KOCGI · Sep 16, 2018 at 12:55 AM 0
Share

This piece of code is used to find the closest enemy in an array of enemies. How you get the array is up to you, and when you trigger this piece of code is up to you.

I would recommend thinking about the piece of code, understanding how it works, and how it could be integrated into your code. Giving you a full piece of code that you can plug-and-play will do very little for your learning.

avatar image KOCGI nathanlink169 · Sep 16, 2018 at 01:58 AM 0
Share

I know how the code works. I've been looking at this for days; https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html The Problem is that it wont work at all! I get target with the function, but not the closest one, just the one that has been spawned first in the scene. After I hit it, the target switches to the second one that spawend. I only call the FindClosestEnemy(). It's returning an Enemy, but not the right one. I just wan't to have this work to finalize this project. this is very frustrating. I don't have time, ether I get it working within the next hours, or it won't be in there at all.

Show more comments

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

177 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 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 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 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

How can I make an object transform to given position and rotation 2 Answers

Transform shows in Scene View on Runtime, but not in Camera (also: Vive Trackers) 0 Answers

How to keep track of multiple NPC's / Enemies 0 Answers

How do I code transform translate in unity3d/ Not working right 0 Answers

transform.lookat not working? 0 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