Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kosmosaik · May 26, 2018 at 01:08 PM · enemytargetingtower defense

My Tower is acting weird (shooting at one enemy, killing different enemy)

Can someone tell me what's wrong here? It looks like my tower is shooting at one enemy, but killing a random enemy on hit. I believe it has something to do with the UpdateRange function.

The code about targeting is more or less copied from Brackey's tower defense tutorial.
The rest of the code I'm sure is a mess as well, but everything else is working for now.

Video to see what's going on: https://youtu.be/maeDYNTvamA
Script:

 using UnityEngine;
 
 public class TowerHandler : MonoBehaviour
 {
 
 
     [Header("Attributes")]
 
     public float attackSpeed = 1f;
     public float range;
     private float fireCountdown = 0f;
 
     [Header("Unity Setup")]
     public Transform enemyTarget;
     public GameObject[] attackPrefabs;
     public Transform firePoint;
     public GameObject rangeModel;
 
     public string enemyTag = "Enemy";
 
 
     private void Start()
     {
         rangeModel = GameObject.FindGameObjectWithTag("Range");
         rangeModel.SetActive(false);
         InvokeRepeating("UpdateTarget", 0f, 0.1f);
     }
 
 
     void UpdateTarget()
     {
         GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
         float shortestDistance = Mathf.Infinity;
         GameObject nearestEnemy = null;
 
         foreach (GameObject enemy in enemies)
         {
             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
             if (distanceToEnemy < shortestDistance)
             {
                 shortestDistance = distanceToEnemy;
                 nearestEnemy = enemy;
             }
         }
 
         if (nearestEnemy != null && shortestDistance <= range)
         {
             enemyTarget = nearestEnemy.transform;
         }
         else
         {
             enemyTarget = null;
         }
 
 
     }
 
     void Update()
     {
         if (enemyTarget == null)
         {
             return;
         }
 
         if (fireCountdown <= 0f)
         {
             Shoot();
             fireCountdown = 1f / attackSpeed;
         }
 
         fireCountdown -= Time.deltaTime;
 
     }
 
     private void Shoot()
     {
         GameObject bulletGO = (GameObject)Instantiate(attackPrefabs[0], firePoint.position, firePoint.rotation);
         Bullet bullet = bulletGO.GetComponent<Bullet>();
 
         if (bullet != null)
         {
             bullet.SeekEnemy(enemyTarget);
         }
     }  

}
And here's the bullet-script:

 public class Bullet : MonoBehaviour {
 
     private Transform target;
     public GameObject impactEffect;   // Byt ut denna beroende på vilken bullet-type/attacktyp det är. Träff-effekt
     GameObject player;
     GameObject enemy;
     EnemyBehavior enemyB;
     TowerAttributes towerAtt;
     public float playerDamage;
 
     public float speed = 70f;
 
 
     public void SeekEnemy (Transform _target)
     {
         target = _target;
     }
 
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
         enemy = GameObject.FindGameObjectWithTag("Enemy");
         enemyB = enemy.GetComponent<EnemyBehavior>();
         towerAtt = player.GetComponent<TowerAttributes>();
         playerDamage = towerAtt.damage;
     }
 
     // Update is called once per frame
     void Update () {
         if(target == null)
         {
             Destroy(gameObject);
             return;
         }
 
         Vector3 dir = target.position - transform.position;
         float distanceThisFrame = speed * Time.deltaTime;
 
         if(dir.magnitude <= distanceThisFrame)
         {
             HitTarget();
             return;
         }
 
         transform.Translate(dir.normalized * distanceThisFrame, Space.World);
     }
 
     private void HitTarget()
     {
         GameObject effectInstance = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
         Destroy(effectInstance, 1f);
         Damage();
         Destroy(gameObject);
     }
 
     void Damage()
     {
         Debug.Log("Damage!");
         enemyB.TakeDamageFromPlayer(playerDamage);
     }
 }







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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

revise enemy code to player code 0 Answers

Targeting script not finding a target 2D Mode 1 Answer

Lock onto a target 1 Answer

TowerDefense Game, shooting at Target! 2 Answers

Need help with tower defense win level system,need help with tower defense win level system 0 Answers


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