- Home /
Auto-targeting help
Hi,
The following script is what I use in an attempt to have an 'auto-target' system. What happens is:
Finds all game objects tagged 'Enemy'.
Calculates the distance between transform(the player) and the game object.
If the distance is less than or equal to 5, change some values.
What I expect to happen:
When player is in range of game object tagged 'Enemy', the player's variable 'target' changes to the object within range.
When player leaves that range, the target goes to null.
When the player reenters ANY range of a object with tag 'Enemy', the player's variable 'target' becomes that object.
What actually happens:
When a player enters a range of an object with tag 'Enemy', the player's variable 'target' becomes that object. GOOD
When the player exits the range, the variable goes to null. GOOD
Reentering the same object's range, rechanges the variable to that object. GOOD
BUT, it only works with one gameobject with tag 'Enemy'. Entering a different object that is identical to the other, will NOT change the player's variable. The only way it will, is if the one that works is destroyed.
Here's the script:
#pragma strict
function Start()
{
}
function Update () {
CheckForMob();
}
function CheckForMob()
{
var targets: GameObject[];
targets = gameObject.FindGameObjectsWithTag("Enemy");
for(var primarytarget in targets)
{
var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position);
if(primtargetdistance <= 5)
{
transform.GetComponent(meleecombat).target = primarytarget.transform;
}
else
{
transform.GetComponent(meleecombat).target = null;
}
}
}
Here's the 'meleecombat' script that this one refers to:
#pragma strict
var target: Transform;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
if(target == null)
{
print("You have no target!");
}
else
{
Attack();
}
}
}
function Attack()
{
var distance: float = Vector3.Distance(transform.position, target.transform.position);
var dir : Vector3 = (target.transform.position - transform.position).normalized;
var direction: float = Vector3.Dot(dir,transform.forward);
var hitnum = Random.value;
if(distance < 2.5 && direction > 0.70)
{
if(hitnum >= 0.19)
{
print("You attacked the target!");
target.GetComponent(enemy).AdjustHealth(-5);
var currhealth : float = target.GetComponent(enemy).currhealth;
DeathCheck(currhealth);
}
else
{
print("You missed the target!");
}
}
}
function DeathCheck(number:float)
{
if(number == 0)
{
Destroy(target.gameObject);
target = null;
}
}
Answer by jonassejr · Aug 26, 2013 at 02:10 PM
your code only works for one enemy, because you continue checking for enemies after you found one, if the last enemy you are checking is to far away you set the target to null regardless of all the previous enemies.
i rewrote your CheckForMob function so it loops through all the enemies and stops when it finds an enemy close enough.
function CheckForMob()
{
var targets: GameObject[];
var target: GameObject;
targets = gameObject.FindGameObjectsWithTag("Enemy");
for(var primarytarget in targets)
{
var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position);
if(primtargetdistance <= 5)
{
target = primarytarget.transform;
break;
}
}
transform.GetComponent(meleecombat).target = target;
}
Thanks alot, I'll look through it, and do the changes, and I'll let you know if it works.
Thanks again
Thanks for your help jonassejr!
All is well, and everything works as expected and as intended.
Good day
Your answer
Follow this Question
Related Questions
Auto Targeting script issue 1 Answer
FindClosestEnemy() change target? 1 Answer
target and look at random enemies 1 Answer
make a ball hit a target 0 Answers
Object Look At Mouse 2 Answers