- Home /
The question is answered, right answer was accepted
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;
}
}
}
}
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;
}
}
}
Follow this Question
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