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 /
This question was closed Sep 16, 2013 at 12:36 PM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DarkWolffe · Aug 26, 2013 at 11:36 AM · javascript

Tracking and targeting one of multiple objects

So here's what I have set up currently:

There are three objects, A B and C, all on a plane. Object A will move about the level if you click a location, much like a dungeon crawler or a MOBA, Object B remains in place unless moved in the Editor, and Object C will move towards objects A and B if they are within a radius of 10.

The script I have set up puts all of the enemies in an array based on their tag and then uses that to calculate the distance of each one in the Update function/method (currently using Javascript, wishing I was using C#, too lazy to change now). If it finds something within its radius, it sets that object as the closest available, sets the bool targetFound to true, and then defines the target variable as whatever the closest is for later use.

Here's my problem:

I want the object to function much like the minions of League of Legends, where they will track object A so long as it is present in their line of sight, and then move on to another, namely B, if A leaves the stage. I just don't know how to set this up in the least. Furthermore, my current code only returns null.

Here's my code:

 #pragma strict
 
 var target;
 var targetFound = false;
 
 function Update() {
     Debug.Log(FindClosestEnemy());
     // if(FindClosestEnemy() != null && targetFound == true) {
     //     transform.position = Vector3.MoveTowards(transform.position, FindClosestEnemy().transform.position, 3 * Time.deltaTime);
     // }
 }
 
 // Find the closest available enemy
 function FindClosestEnemy () : GameObject {
     var gos : GameObject[];
     gos = GameObject.FindGameObjectsWithTag("Champion");
     
     var closest : GameObject = null;
     var los : float = 50;
     var position = transform.position;
     
     // Check every instance of the array of enemy characters
     for(var go : GameObject in gos) {
         // Calculate difference in distance between the objects
         var diff = (go.transform.position - position);
         // Equate the actual current distance
         var curDistance = diff.sqrMagnitude;
         // Check if the current distance is less than the line of sight
         if (curDistance < los) {
             // Check if we're already targeting something else
             if(targetFound != false) {
                 // Enemy must be nearby
                 closest = go;
                 // We are now targeting that enemy
                 targetFound = true;
                 // Declare the target
                 target = closest;
                 // We're kinda done here
                 return target;
             // Otherwise, we must already have a target
             } else {
                 return target;
             }
         }
     }
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by DarkWolffe · Aug 28, 2013 at 06:57 AM

I figured it out. I had to set the variable closest on start because it was being reset every frame. This is the code:

 #pragma strict
 
 var closest : GameObject;
 
 function Start() {
     closest = null;
 }
 
 function Update() {
     FindClosestEnemy();
 }
 
 // Find the closest available enemy
 function FindClosestEnemy () : GameObject {
     var los : float = 50;
     var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Deity");
     var position : Vector3 = transform.position;
     
     for(var enemy : GameObject in enemies) {
         var diff = (enemy.transform.position - position);
         var curDistance = diff.sqrMagnitude;
         if (curDistance < los) {
             if(closest == null) {
                 closest = enemy;
                 Debug.Log(closest);
             } else {
                 Debug.Log("Already found something to target.");
             }
         } else {
             closest = null;
         }
     }
 }
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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

how can i make a circle slider in unity3d for movement like this image.thanks 0 Answers

Issue using .Contains with application.loadedlevel/loadlevel 1 Answer

Create reset button to main camera 1 Answer

How to create a UnityScript array and access the data in each cell. 1 Answer

How to handle 5 touches at a time? 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