Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hypnotoad0 · May 16, 2014 at 08:26 PM · enemytagdamagetarget

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);
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
    }
  }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hypnotoad0 · May 16, 2014 at 09:14 PM 0
Share

This is extremely useful, thanks a lot.

avatar image
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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hypnotoad0 · May 16, 2014 at 09:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Ray Cast Click on Enemy - Change Current Target 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges