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 ZWLSOFTWARE · May 16, 2020 at 05:40 PM · 2dphysics2dsystemturret

2D turret system (detection problem)

Hi, I'm having some problems with detecting a transform inside a range.

When i use this script the turret will detect the enemy. But if there are multiple enemies the turret goes crazy because it changes [0] transform gameObject.....

Is there a way to detect an enemy and wait before switching the [0] or transform if the enemy leaves the area or gets killed?


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyQueue : MonoBehaviour
 {
 
    public LayerMask whoisenemy;
     public Vector3 turrentPos;
  
   public  float range; 
    public Collider2D[] enemyQueue;
     public Transform enemyTransform;
  
     void Update()
     {
 
         enemyQueue = Physics2D.OverlapCircleAll(turrentPos, range, whoisenemy);
 
         for (int i = 0; i < enemyQueue.Length; i++)
         {
 
 
 
             enemyTransform = enemyQueue[0].transform;
 
 
 
         }
 
 
 
     }
 
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.yellow;
          Gizmos.DrawWireSphere(turrentPos, range);
 
     }
 }
 












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

1 Reply

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

Answer by JxWolfe · May 16, 2020 at 09:22 PM

An easy way of doing this is changing Enemyqueue to a List - then adding enemies to that list. (if enemy == null then remove the enemy) then use [0] for the target

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 ZWLSOFTWARE · May 17, 2020 at 11:44 AM 0
Share

Thank you, I used System.Linq And I think it I don't see any switching with my transform now. But When the enemy goes out of range the Turret still detects it. How do I set the enemy to Null when it's out of range? Here's the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 public class EnemyQueue : $$anonymous$$onoBehaviour
 {
 
    public Layer$$anonymous$$ask whoisenemy;
     public Vector3 turrentPos;
  
   public  float range; 
    public List <Collider2D> enemyQueue;
     public Transform enemyTransform;
       
     void Update()
     {
 
         enemyQueue = Physics2D.OverlapCircleAll(turrentPos, range, whoisenemy).ToList();
          
         
         foreach (Collider2D col in enemyQueue)
         {
 
             Debug.Log("FOUND: " + col.gameObject.name);
             Debug.Log(enemyQueue.Count + col.gameObject.name);
 
 
             if (enemyQueue.Count == 1) { //see who is first
                 enemyTransform = col.gameObject.transform; 
 
             }
 
             if(col.gameObject.transform != enemyTransform)
             {
                 Debug.Log("changed transform");
                 enemyQueue.Remove(col);

             }
        
      
             if (col.gameObject.name == "GameObject") {
 
                 enemyQueue.Remove(col); // this is an extra collider I added to the enemy. I only want the Enemy collider in the list               
             }
          
           
         }
       
 
 
     }
 
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.yellow;
          Gizmos.DrawWireSphere(turrentPos, range);
 
     }
 }
 



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

270 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 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 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 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

Creating a movement script to move up and down a chain (or rope) in a 2D game 1 Answer

2D Ground, really long, how? 1 Answer

Unity 5 2D platformer jumping problem after porting 0 Answers

Staring system, problem... 2 Answers

Physics2D checking if object goes out of OverlapArea 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