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 LeftyTwoGuns · Aug 18, 2013 at 01:56 AM · collidertargetoverlapsphere

OverlapSphere not detecting target

I have an automatic turret I would like to aim at specific enemies and prioritize shooting the closest enemy in its OverlapSphere. I got it to aim at the enemies just fine but it doesn't seem to be picking them up in the Inspector. Did I get everything right in my script?

 using UnityEngine;
 using System.Collections;
 
 public class Cannon : MonoBehaviour {
     
     private GameObject target;
     public float Range = 5;
     private Quaternion targetRotation;
     public bool Aiming = false;
     public Collider[] Targets;
 
     
     
     // Update is called once per frame
     void Start () {
         
          target = GameObject.FindWithTag("Enemy");
     }
     
     Transform FindTargets(){
         Transform nearest = null;
         Collider[] Targets = Physics.OverlapSphere(transform.position, Range);
         foreach (Collider hit in Targets){
             if(hit && hit.tag == "Enemy"){
                 float dist = Vector3.Distance(transform.position, hit.transform.position);
                 if(dist < Range){
                     Range = dist;
                     nearest = hit.transform;
                 }
             }
         }
         return nearest;
         
     }
  
     void Update(){
         
         if(Aiming){
     
        Vector3 look = target.transform.position - transform.position;
        look.z = 0;
        targetRotation = Quaternion.LookRotation (look);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
     
         Transform enemy = FindTargets ();
         if(enemy){
             Debug.Log("BANG");
         }
   }
     
     }
     
 }
Comment
Add comment · Show 1
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 Linus · Aug 18, 2013 at 02:49 AM 0
Share

Have to ask, have you remembered to set the "Enemy" tag on the object?

Try this debug: foreach (Collider hit in Targets){ Debug.Log("Hitted:"+hit.transform.name);

1 Reply

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

Answer by robertbu · Aug 18, 2013 at 04:02 AM

I see three problems with your code.

  1. You declare 'public Collider[] Targets' on line 10, then on line 22, you declare 'Collider[] Targets'. The second one is local to FindTargets() and therefore overrides the one on line 10. This mean the one on line 10 is never accessed. That is why you don't seen any values in the inspector. You need to remove the 'Colider[]' from the one on line 22 in order for the one on line 10 to be assigned.

  2. On line 27, you write, 'Range = dist'. This code means that you are overwriting the value of 'Range' set in the inspector. The first time through the value gets dropped to the distance to the nearest object. After that, Physics.OverlapSphere() will only change if other objects are nearer the the nearest object. That is, it will not find targets that are within the original range but further than the last nearest target even if previous nearest target no longer exists or has moved out of range.

  3. In Update() you never assign the results of 'FindTargets()' to 'targtet', therefore the code never finds any target but the first one.

Here is a bit of a rewrite:

 Transform FindTargets(){
        float maxDist = Range;
    Transform nearest = null;
    Targets = Physics.OverlapSphere(transform.position, Range);
         Debug.Log (Targets.Length+","+Range);
    foreach (Collider hit in Targets){
      if(hit && hit.tag == "Enemy"){
       float dist = Vector3.Distance(transform.position, hit.transform.position);
       if(dist < maxDist){
           maxDist = dist;
           nearest = hit.transform;
       }
      }
    }
    return nearest;
 }

Note when you rewrite Update(), make sure you assign null values returned by FindTargets() to be assigned to 'target'. If you don't, your gun will continue to track the last target even if it is out of range, and it will also attempt to track the last target even when all the enemies are destoryed. Of course if you allow null values for 'target', you will need to avoid accessing target when it is null.

Comment
Add comment · Show 4 · 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 Bunny83 · Aug 18, 2013 at 04:23 AM 0
Share

Perfect summary ;) +1

avatar image LeftyTwoGuns · Aug 18, 2013 at 05:26 AM 0
Share

Thank you so much! I was aware of solutions 1 and 2, but I wasn't sure if those lines needed to be re-stated or not so I figured better safe than sorry. Not in this case it seems.

As far as the third solution, is there a better way to keep track of targets than assigning null values? I've seen some examples of OverlapSpheres using for() statements but I don't understand their use. The only targets the turret will be ai$$anonymous$$g at are children of an empty object prefab that instantiates when another object is destroyed.

avatar image robertbu · Aug 18, 2013 at 05:35 AM 1
Share

Null is fine. I don't know your game mechanic, but I expect the turret to stop tracking if the player is out of range. If so, then your logic will be something like (pseudo-code):

 target = FindTargets();
 
 if (target != null) {
     // do the rotating and firing stuff;
 }

This way, the turret stops firing if all the player object are out of range or they are all destroyed, and it immediately starts firing and tracking if any move back in range.

avatar image LeftyTwoGuns · Aug 21, 2013 at 09:46 PM 0
Share

$$anonymous$$essing around a little bit, everything seems to be working as intended. The new update is void Update(){

         Transform target = FindTargets();
         if(target != null){
             
                 
                Vector3 look = target.transform.position - transform.position;
                look.z = 0;
                targetRotation = Quaternion.LookRotation (look);
                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
             
             }
                 
             
         }

As far as I can tell the turret appears to be changing targets correctly. So thanks a bunch!

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

17 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

Related Questions

HELP! i want to write to look at the target and give a force to follow 3 Answers

Does OverlapSphere() return the colliders in any particular order? 1 Answer

Destroy objects within ordered Array by line of sight 1 Answer

targeting multiple enemys 1 Answer

OverlapSphere ignoring all colliders when I use the layerMask parameter. 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