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 Lemo76 · Nov 06, 2014 at 09:01 PM · javascriptgameobjecttransformarrayposition

Transform Checking on all Array Objects (JS)

Hello Unity helpers, The scenario goes as follows:

I have Ghost Objects being created with a specific 'y' value on the transform.position I have an array which finds all the Ghost Objects in the scene

 GhostFound = GameObject.FindGameObjectsWithTag("GhostTag");
 for (var i=0; i < GhostFound.length; i++)
   {
     SpecificGhost = GhostFound[i];
   }

So my question is, how would I get all of the values of the y transform position on all the ghosts in the scene (from that array), and if at least one of the y values matches the number '860', it sets the 'GhostonPlat' boolean to true?

I really do appreciate your help!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Nov 06, 2014 at 09:14 PM

Direct comparison to floats often fails to to floating point imprecision. You can use Mathf.Approximately(), or if there is more chance of imprecision (i.e. moving using Time.deltaTime(), you can look for range. So:

 for (var i=0; i < GhostFound.length; i++)
 {
     var go = GhostFound[i];
     if (Mathf.Abs(go.transform.position - 860.0) < someThresholdValue) {
           go.GetComponent(Ghost).GhostonPlat = true;
     }
 }

This assumes that the script with the 'GhostonPlat' variable is named 'Ghost'. Change as appropriate. 'someThresholdValue' will be some small value you set that defines how much the position can vary from 860.0 and still be considered 860.0.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

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 robertbu · Nov 06, 2014 at 10:16 PM 0
Share

Reading the other answers, I assumed that the 'GhostonPlat' boolean was on the game object, not in this file. If it is in this file, then line 5 would be just:

   GhostPlat = true;
avatar image
0

Answer by Uldeim · Nov 06, 2014 at 09:21 PM

First of all, I'd highly recommend NOT doing FindGameObjectsWithTag (or any version of Find anything) pretty much ever, but definitely not during an Update loop. I'd instead recommend adding them to some sort of variable thing when you create them.

Ok, sorry about that. Answer time.

 var MagicNumber = 860;
 var GhostYs = [];
 GhostFound = GameObject.FindGameObjectsWithTag("GhostTag");
 for (var i=0; i < GhostFound.length; i++)
 {
    SpecificGhost = GhostFound[i];
    GhostYs.push(SpecificGhost.transform.y);
    if (SpecificGhost.transform.y == MagicNumber)
    {
       GhostOnPlat = true;
       break;
    }
 }


Fairly simple comparison; grab the y value of the transform and compare it (to a variable or const, not a numeric literal cough cough), set the bool and break out of the loop. Break terminates the loop early; if you care about checking every Ghost even after finding one, you can leave it out and the code should work just the same.

You can see I've pushed the Ghost Y values onto an array as well, though if all you needed was to check that one boolean, it's not really necessary.

EDIT: @robertbu has an excellent point. Definitely use some form of float approximation for equals regardless of what you do.

Comment
Add comment · 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
0

Answer by FairGamesProductions · Nov 06, 2014 at 10:10 PM

Simpler solution:

 private var GhostsFound : GameObject[];
 private var GhostonPlat = false;
 
 function Start ()
     {
     GhostsFound = GameObject.FindGameObjectsWithTag("GhostTag");
     for (i in GhostsFound)
         {
         if (i.transform.localPosition.y == 860)
             {
             GhostonPlat = true;
             }
         }
     }

Sometimes in Unity the locations of gameObjects changes by 0.000000001. So checking directly equals on a transform is not recommended. You should probably check like this:

 if (i.transform.localPosition.y > 860.5 && i.transform.localPosition.y < 859.5)
Comment
Add comment · Show 2 · 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 Uldeim · Nov 06, 2014 at 10:27 PM 0
Share

I'd be worried about using localPosition ins$$anonymous$$d of just position...

avatar image FairGamesProductions · Nov 06, 2014 at 10:32 PM 0
Share

Well, it depends on what he needs. He can use either localposition or position. The principle stays the same.

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

28 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

Related Questions

How do I create an array for multiple targets? 1 Answer

Instantiate as a child at position 2 Answers

problem moving a prefab object in script (c#) 0 Answers

Position of a GameObject 2 Answers

Locating Position of Object not Working? 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