• 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
1
Question by Rush3fan · May 03, 2012 at 09:44 AM · obstacleexcludeself

Finding the closest obstacle to you when you are an obstacle.

Hi. I'm trying to t$$anonymous$$nk of a way to find all obstacles in my game that use the obstacle tag. The confusing part is that my player is an obstacle to other players, but there is no point in counting yourself as an obstacle, so I need to exclude myself in t$$anonymous$$s script. Have any idea how? All I need to know is the closest obstacle to me other than myself.

 function FindThatObstacle():GameObject{
 
     var gos:GameObject[];
     gos=GameObject.FindGameObjectsWithTag("Obstacle"); 
     var closest:GameObject; 
     var distance=Mathf.Infinity; 
     var position=transform.position; 
     // Iterate through them and find the closest one
     for (var go : GameObject in gos)  { 
         var diff=(go.transform.position - position);
         var curDistance=diff.sqrMagnitude; 
         if (curDistance<distance) { 
             closest=go;
             distance=curDistance; 
         } 
     } 
     return closest;    
 }
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 Rush3fan · May 03, 2012 at 10:39 AM 0
Share

I'm also trying to get it to use Vector3.Dot() to compare the forward direction instead, but one thing at a time.. so basically the nearest obstacle in front of you and the ones behind ignored.

1 Reply

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

Answer by Bunny83 · May 03, 2012 at 10:41 AM

 // [...]
 var position=transform.position; 
 // Iterate through them and find the closest one
 for (var go : GameObject in gos)  {
     if (go.transform != transform) {
         var diff=(go.transform.position - position);
         var curDistance=diff.sqrMagnitude; 
         if (curDistance<distance) { 
             closest=go;
             distance=curDistance;
         }
     }
 }

T$$anonymous$$s should do the trick ;) I'm not sure if UnityScript knows continue but it's a quite basic keyword so it should work. I'm a C# user ;)

Comment
Add comment · Show 10 · 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 Rush3fan · May 03, 2012 at 10:42 AM 0
Share

Thanks! let me try it out here..

avatar image Rush3fan · May 03, 2012 at 10:55 AM 0
Share

Hmm... it still seems to return the closest gameobject which is it's self.

avatar image Rush3fan · May 03, 2012 at 10:58 AM 0
Share

Should I try break; instead of continue;?

avatar image Bunny83 · May 03, 2012 at 10:58 AM 0
Share

That doesn't make sense ;) Are you sure UnityScript compiles the script? No errors? You could also invert the condition and surround all the code with it. I'll change the example...

avatar image Bunny83 · May 03, 2012 at 01:05 PM 1
Share

Have you tried printing the name of the closest object? Also make sure any child objects of your player arn't tagged "Obstacle". They would be found as well.

You could also check like this

 if (go.transform.IsChildOf(transform))
     continue;

IsChildOf checks if the object is a child, deep child or the transform itself just to be on the sure side ;)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

a one time script 1 Answer

please help 1 Answer

How do I access the object the script is attached to? 3 Answers

Create Legacy Ellipsoid Emitter from script 0 Answers

Move from point A to point B, then destroy? ( java S) 2 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