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
1
Question by Ufando · Nov 07, 2012 at 12:49 AM · playerobjectdistancebetween

Check for an instantiated object farthest from player?

I'm trying to make a script that when the player moves a certain distance from any gameobject with a certain tag, SetActiveRecursively(false), but only deactivate the object farthest away from the player.

 e.g (P is player)
 |  ||  | | P | |  |
          <--
 when the player moves to here
 
 |  || P| |  | |  |
 set this to false
 |  ||P | |  | | X |


How would i go about doing this?

Comment
Add comment · Show 4
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 Seth-Bergman · Nov 07, 2012 at 01:06 AM 0
Share

what do you mean "only deactivate the object farthest away from the player".. You only want ONE object to deactivate? Or each one once it's far enough away?

avatar image Ufando · Nov 07, 2012 at 01:31 AM 0
Share

Each one once it's far enough away

avatar image Seth-Bergman · Nov 07, 2012 at 01:54 AM 0
Share

One other question.. when you get closer to that object again, should it turn back on? or once it's deactivated, it's gone for good?

OR, do you mean that only the furthest object should be turned off at any given time?

avatar image Ufando · Nov 07, 2012 at 02:12 AM 0
Share

when you get closer it should turn back on, that's why i was using SetActiveRecursively rather than just destroying it.

1 Reply

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

Answer by Seth-Bergman · Nov 07, 2012 at 02:28 AM

OK, well, if you are trying to keep track of All the objects of a given tag, first you will need an array of all objects with that tag.. then you will cycle through the array, checking the distance of each from the player, and setting them accordingly:

 var myObjects : List.<GameObject> = new List.<GameObject>();
 var maxDistance : float = 20.0;
 
 function Update(){
    //GetObjects();
    for (var obj in myObjects)
    {
    if(Vector3.Distance(transform.position,obj.transform.position) > maxDistance && obj.active){
    obj.SetActiveRecursively(false);
    Debug.Log("Distance is: " + Vector3.Distance(transform.position,obj.transform.position));
    }
    else if(Vector3.Distance(transform.position,obj.transform.position) <= maxDistance && !obj.active)
    obj.SetActiveRecursively(true);
    }
 }

 function GetObjects(){
 var tempObjects = GameObject.FindGameObjectsWithTag ("SomeTag");
   for(myObj in tempObjects){
      if(!myObjects.Contains(myObj))
      myObjects.Add(myObj);
   }
 }

you could uncomment the first line in Update, to make this work, but it is more efficient to only call this relatively expensive function as necessary.. so, if you can instead call the function "GetObjects()" only once each time you INSTANTIATE a new object, that will be more efficient.. for now, however, go ahead and un-comment the line in Update, if you like..

EDITED: forgot to mention,this script would be attached to the player.. using a list gives us the best control, so this should work now I think.. The debug should tell you the distance if it turns anything..

Comment
Add comment · Show 5 · 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 Ufando · Nov 07, 2012 at 02:43 AM 0
Share

Thanks for the reply, when i play however, the size of the array immediately goes to 0. All the prefabs that i have instantiated have the same tag, which is the one i'm checking for in the start function.

EDIT: I tried putting it in the update function, which means now the objects deactivate, but they all seem to deactivate no matter where the player is, even if i mess around with the maxDistance variable.

avatar image Seth-Bergman · Nov 07, 2012 at 02:45 AM 1
Share

no prob, let me update my answer above to make it clearer..

O$$anonymous$$, edited.. Basically, it can be costly using GameObject.FindGameObjectsWithTag EVERY frame, and it's not really necessary here since you can call it each time you instantiate a new object

(AFTER the new object is instantiated, you call the function to update the list)

for now though, just un-comment the first line of Update, and try it that way

avatar image Ufando · Nov 07, 2012 at 03:01 AM 0
Share

If it's in update it still deactivates all of them, but when it isn't, i noticed that(because the array size is always resetting to 0 when i play) if i manually add something to the array, no matter the max distance(even at something like 100000), it deactivates whatever object is in the array.

avatar image Seth-Bergman · Nov 07, 2012 at 03:10 AM 1
Share

O$$anonymous$$, maybe FindGameObjectsWithTag only finds ACTIVE objects then.. let me change one more thing...

O$$anonymous$$, have a look now, I think this should do it.. ..make sure the script is attached to the player (forgot to mention that before), and if it still turns something off, it should log the distance

avatar image Ufando · Nov 07, 2012 at 03:36 AM 0
Share

YES! That works! Thanks a heap you've been very helpful! (i orignally thought it wasn't working but i forgot to resave the script >_<)

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

10 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

Related Questions

How to do something when an object is in the player sights. 2 Answers

Distance to object and rotation lock 1 Answer

How to use tag for all players in the Instantiate. 0 Answers

help loading level after destroying objects 1 Answer

Camera Problem 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