Hi all,
I have some prefabs of enemies running along the ground that I want my ship to be able to identify as an enemy, with a beam of light as it cruises over them.
I wrote the Raycast script and tagged the prefab “enemy” . When I fly over each one of them and are within a certain distance, a Raycast line should appear from the ship to the enemy.
The problem is the line is cast over just one enemy the entire time and is usually the farthest in one in the 2D side-scroll game view.
I want it to cast a line to each one as the ship flies over it, so after passing each enemy and going out of range it should lock onto the next enemy once in range.
It doesn’t do this. Does anyone know is wrong?
Use a function like this (I think it is from the Unity documentation pages) but maybe you have to make changes to fit in your project:
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[ ];
gos = GameObject.FindGameObjectsWithTag(“Enemy”);
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
miPos = go.transform.position;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
Thanks for the code, I’ve been playing around with it and not getting anything. no errors, no action. strange.
I added a bit of code I found on the forums but the print statement doesn’t show up at all.
function CheckForEnemies(){
while( true ){
print(FindClosestEnemy().name);
yield new WaitForSeconds( 0.5 );
}
}
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("cow");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
//miPos = go.transform.position;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
This code example is from the documentation.(Script Reference → FindGameObjectsWithTag) It uses the same code like yours:
// Instantiates respawnPrefab at the location
// of all game objects tagged “Respawn”.
var respawnPrefab : GameObject;
var respawns = GameObject.FindGameObjectsWithTag (“Respawn”);
for (var respawn in respawns)
Instantiate (respawnPrefab, respawn.position, respawn.rotation);
// Print the name of the closest enemy
print(FindClosestEnemy().name);
// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var gos : GameObject[ ];
gos = GameObject.FindGameObjectsWithTag(“Enemy”);
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}