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 siege911 · Jan 12, 2012 at 11:17 PM · lookatphysics.overlapsphere

Weird behavior finding target using Physics.OverlapSphere

I'm trying to learn Unity and making a simple Tower Defense game to do it. The towers have a radius in which they can attack. I'm using Physics.OverlapSphere to find available targets and then checking which target is closest. That target becomes the target until he leaves the tower's range, after which it reruns the function to find a new target.

Almost everything seems to be working properly. I'm using gizmos to visually display the target and that works great. The only thing that isn't working is I want the tower to LookAt the object. If I set the tower attack radius (detectionRange) to a high number like 20 or more which takes up most of my map, the tower will rotate properly only when the targets get really close (5 or less). If the radius is more reasonable like 5, then the gizmos and targeting works but the tower doesn't rotate.

What am I doing wrong?

Here's the code (also since I'm learning, please feel free to tell me where I could do things better):

 #pragma strict
 
 var hasTarget : boolean = false;
 var detectionRange : float;
 private var closestEnemy : Transform;
 
 function Update () {
     
     //Makes sure we have a target
     if (hasTarget){
         //Checks the distance between the tower and target
         var currentEnemyDist : float = (closestEnemy.position - transform.position).sqrMagnitude;
         //If the target is further than the detectionRange, get a new target
         if (currentEnemyDist > detectionRange) {
             hasTarget = false;
         }
     }
     //If we have a target, rotate to look at it
     if(hasTarget){
         transform.LookAt(closestEnemy.transform);
     }
     //If we don't have a target, find one
     else {
         GetTarget();
     }
 }
 
 function GetTarget() {
     //Create a sphere with radius of detectionRange and return targets in range
     var enemiesInRange : Collider[] = Physics.OverlapSphere(transform.position, detectionRange);
     var enemyDistance : float = Mathf.Infinity;
     var colDistance : float;
     //Cycle through each target in the sphere
     for (var col : Collider in enemiesInRange) {
         //Targets must have the tag "Enemy"
         if (col.gameObject.tag == "Enemy") {
             //Check the distance from the tower to the target
             colDistance = (col.transform.position - transform.position).sqrMagnitude;
             //Finds the closest enemy
             //Checks each target to see if the next one is closer than the previous
             if(colDistance < enemyDistance) {
                 //If it's closer, set the target
                 closestEnemy = col.transform;
                 //Set the distance to check against subsequent potential targets
                 enemyDistance = colDistance;
                 //Set the tower to know it has a target
                 hasTarget = true;
             }
         }
     }
 }
 
 function OnDrawGizmos () {
     Gizmos.color = Color.blue;
     if(hasTarget) {
         // Draws a blue line from this transform to the target
         Gizmos.DrawLine (transform.position, closestEnemy.position);
     }
     //Draws a sphere to see the detectionRange
     Gizmos.DrawWireSphere (transform.position, detectionRange);
 }
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 Berenger · Jan 13, 2012 at 02:25 AM

Vector3.Distance(A, B) instead of (A - B).sqrMagn seems to solve the problems when I use your script. As to know why ... [EDIT] Ok, I think when you want a detectionRange of 10, because of .sqrMagnitude, you detect only the enemies at sqrt(10).

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 siege911 · Jan 13, 2012 at 03:23 AM 0
Share

Looks like that worked. I was using sqr$$anonymous$$agn from another script I found here but I was a little confused as to it's exact function. Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to get an object to "LookAt" the first collided object(Physics.OverlapSphere) 0 Answers

[RePost for No Answer] How to make a character look at the direction it's going towards 1 Answer

replace raycast with transform direction 1 Answer

Indicator rotating around player 1 Answer

look at player when button pressed 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