- Home /
Only damage one object with "Enemy" tag
Im making a tower defense game and i need my cannon to pick a target (first enemy that enters it's range) and shoot at it until it dies while ignoring other enemies, then pick a new target. Right now my turret damages every enemy with the tag "Enemy" that enters it's range.
My cannon code:
var level = 1;
var damage = level;
var attackspeed = 1;
var isshooting = 0;
var currentenemy : GameObject;
function Start () {
}
function Update () {
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.CompareTag("Enemy")){
print("damaged the enemy");
other.gameObject.SendMessage("OnDamage", damage);
}
}
And my Enemy code:
var health=10;
function OnDamage(damage:int)
{
print(damage);
health = health - damage;
if(health <- 0)
{
Destroy(gameObject);
}
}
Answer by CodeElemental · May 16, 2014 at 08:57 PM
Since i'm not experienced that much in javascript i'll provide the code in C#. First you need to implement a list of targets , and the target acquisition mechanism. Here's a simple implementation for it (possible syntax errors may be present but the general concept is solid.)
List<Transform> enemiesInRange = new List<Transform>(); // All the enemis in range
Transform lockedEnemy = null; // Enemy that we're targeting
float firingInterval = 2f; // Fire every 2 seconds.
float elapsedTime = 0f; // Time measuring variable.
void OnTriggerEnter(Collider other) {
// if enemy enters range
if(other.gameObject.CompareTag("Enemy")) {
// and we have no lock on enemy
if (lockedEnemy == null) {
// lock on the entering enemy
lockedEnemy = other.transform; // other.gameObject.transform;
}
// Add it to the list of enemies
enemiesInRange.Add(other.transform);
}
}
void OnTriggerLeave(Collider other) {
if (lockedEnemy == other.transform) {
// if the enemy leaving the range is the one we're targeting remove it from the enemies in range
lockedEnemy = null;
// Reset the aiming timer
elapsedTime = 0;
}
enemiesInRange.Remove(other.transform);
// reacquire target from the enemies in range (the first one in the list).
if (enemiesInRange.Count > 0) {
lockedEnemy = enemiesInRange[0];
} else {
// if no enemies are in range , set the locked enemy to null
lockedEnemy = null;
}
}
}
void Update() {
// if we have locked in enemy.
if (lockedEnemy != null) {
// increase the timer
elapsedTime += Time.deltaTime;
// if enough time has passed , fire
if (elapsedTime > firingInterval) {
// Fire!
lockedEnemy.gameObject.SendMessage("OnDamage", damage);
// Reset the time measuring to start firing
elapsedTime = 0;
}
}
}
Answer by CorruptedTNC · May 16, 2014 at 08:57 PM
All the enemies are being damaged because you're telling it to damage them whenever one enters the range.
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.CompareTag("Enemy"))
{
print("damaged the enemy");
other.gameObject.SendMessage("OnDamage", damage);
}
}
OnTriggerEnter does exactly what it says, it runs when something enters a trigger. The code inside it will only be activated when your enemy enters the area and this snippit of code doesn't actually use currentEnemy anywhere.
You could set OnTriggerEnter to set the currentEnemy to the new one that has entered if it doesn't currently have a target like this:
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.CompareTag("Enemy"))
{
if(currentEnemy == null)
{
currentEnemy = other;
}
}
}
This however wouldn't automatically assign the new enemy when currentEnemy is defeated. I suggest that to work around these issues you look in to using Coroutines as they allow you to loop tasks like damaging an enemy on a time of your choosing. Your code for damaging "currentEnemy" would be placed inside your couroutine.
As for picking a new enemy, you need some way of calculating which enemy to target after your current target dies. I would suggest working out a way of calculating which enemy is furthest along the path, unless you're satisfied with the turret only being able to see an enemy that enters the trigger.
I kind of got it to work, ill try your way if $$anonymous$$e doesnt work out (and it probably wont :D). Thanks a lot for the insight on Coroutines.
Your answer
Follow this Question
Related Questions
Targetting Script Help 1 Answer
Target distance would only update closer 1 Answer
Coding equivalent of Draging an Object into the Inspector (Solved) 1 Answer
Player wont take damage 2 Answers