- Home /
Targeting nearest enemy !
Hi there ! Yeah so, after quite 2 days of searching testing etc... I don't figure it out on what to do for targeting my player on the nearest enemy. I tried to pick up code for another question, testing etc... nothing :/ Here's my code, a bit messy sorry ! #pragma strict
var distance : int;
var lookAtDistance = 5;
var target : GameObject;
var targets : GameObject;
var damping = 6.0;
var fireSpeed : float = 15;
private var waitTillNextFire : float = 0;
private var canShoot : boolean = false;
var bullet : GameObject;
var bulletSpawn : GameObject;
var bulletSound : GameObject;
var muzzleFlash : GameObject;
var reloading : boolean = false;
var reloadingAnim : Animation;
var reloadingAnimString : String;
var reloadingSound : AudioSource;
var magSize : int = 15;
var currentMag : int = 15;
var maxExtraAmmo : int = 250;
var currentExtraAmmo : int = 250;
var bulletHudTexture : Texture;
var ammoCountRect : Rect = Rect(25,25,50,25);
var ammoStartX : int = 100;
var ammoY : float = 25;
var ammoSize : Vector2 = Vector2(10,31);
var ammoSpacing : float = 4;
var ammoHudTexture : Texture;
var ammoHudRect : Rect = Rect(25,50,50,25);
function Start ()
{
var targets = GameObject.FindGameObjectsWithTag("Enemy");
Debug.Log("number of enemies found: "+targets.length);
}
function Update ()
{
if(target != null)
{
Aiming();
}
else
{
canShoot = false;
GameObject.Find("player").GetComponent(playerMovement).enabled = true;
Debug.Log("Toast");
}
if(!reloading && Input.GetButtonDown("Reload") && currentMag < magSize && currentExtraAmmo > 0)
{
reloading = true;
reloadingAnim.Play(reloadingAnimString);
reloadingSound.Play();
}
if(!reloading && Input.GetButtonDown("Fire1") && currentMag == 0 && currentExtraAmmo > 0)
{
reloading = true;
reloadingAnim.Play(reloadingAnimString);
reloadingSound.Play();
}
if(reloading && !reloadingAnim.IsPlaying(reloadingAnimString))
{
if(currentExtraAmmo >= magSize - currentMag)
{
currentExtraAmmo -= magSize - currentMag;
currentMag = magSize;
}
if(currentExtraAmmo < magSize - currentMag)
{
currentMag += currentExtraAmmo;
currentExtraAmmo = 0;
}
reloading = false;
}
if(currentMag > magSize)
currentMag = magSize;
if(currentExtraAmmo > maxExtraAmmo)
currentExtraAmmo = maxExtraAmmo;
if(currentMag < 0)
currentMag = 0;
if(currentExtraAmmo < 0)
currentExtraAmmo = 0;
var holdSound : GameObject;
var holdMuzzleFlash : GameObject;
if(Input.GetButtonDown("Fire1") && currentMag > 0 && !reloading && canShoot == true)
{
if(waitTillNextFire <= 0)
{
currentMag -= 1;
if(bullet)
{
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if(bulletSound)
{
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if(muzzleFlash)
{
holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
waitTillNextFire = 1;
}
}
waitTillNextFire -= Time.deltaTime * fireSpeed;
if(holdSound)
{
holdSound.transform.parent = transform;
}
if(holdMuzzleFlash)
{
holdMuzzleFlash.transform.parent = transform;
}
}
function OnGUI ()
{
if(Input.GetKey(KeyCode.Tab))
{
for (var i : int = 1; i <= currentMag; i++)
{
GUI.DrawTexture(Rect(ammoStartX + ((i - 1) * (ammoSize.x + ammoSpacing)), ammoY, ammoSize.x, ammoSize.y), bulletHudTexture);
}
GUI.Label(ammoCountRect, currentExtraAmmo.ToString());
GUI.DrawTexture(ammoHudRect, ammoHudTexture);
}
}
function LookAt()
{
var rotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function Aiming()
{
for(var enemy : GameObject in targets)
{
distance = Vector3.Distance(enemy.transform.position, transform.position);
if(Input.GetKey(KeyCode.Mouse1) && distance < lookAtDistance)
{
target = enemy;
Debug.Log("My closest selected enemy so far is: "+target.transform.name);
LookAt();
canShoot = true;
GameObject.Find("player").GetComponent(playerMovement).enabled = false;
Debug.Log("Test");
}
else
{
canShoot = false;
GameObject.Find("player").GetComponent(playerMovement).enabled = true;
Debug.Log("Toast");
}
}
}
Here's also topic that help me a little bit and where I pick up some line of code etc... I know that it's C# on the topic but I tried to rewrite it in javascript.
http://forum.unity3d.com/threads/select-target-from-nearest-enemy.8343/
thanks !
Answer by Jeff-Kesselman · Jun 23, 2014 at 11:05 PM
The distance between any two vectors A and B is the magnitude of the vector A-B
So, loop through all your enemies, subtract your position from their position and compare the magnitudes of the resulting vectors.
You can do the distance between two vectors, supposing that we are working in two dimensions, we can do:
distance = sqrt((pointA - pointA0)^2 + (pointB - pointB0)^2));
You can implement the calculus above and get the distance between a point A and B.
Well, in my script I already had this
function Ai$$anonymous$$g()
{
for(var enemy : GameObject in targets)
{
distance = Vector3.Distance(enemy.transform.position, transform.position);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1) && distance < lookAtDistance)
{
target = enemy;
Debug.Log("$$anonymous$$y closest selected enemy so far is: "+target.transform.name);
LookAt();
canShoot = true;
GameObject.Find("player").GetComponent(player$$anonymous$$ovement).enabled = false;
Debug.Log("Test");
}
else
{
canShoot = false;
GameObject.Find("player").GetComponent(player$$anonymous$$ovement).enabled = true;
Debug.Log("Toast");
}
}
}
It's not that ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613152341im_/https://answers.unity.com/themes/thub/images/avi.jpg)