Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pekira · Jun 11, 2017 at 09:19 AM · c#unity 5vector3

Function to cheack if there is already a gameobject in a indicated vector not working

Hello i have the following function that will retrieve a true of false statement depending if there is already a gameobject with a tag of tiles in a de-terminated location

     bool CheackGameObjectPos(Vector3 Pos)
     {
         GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
         foreach (GameObject Game in Array)
         {
             Vector3 ZeroPos = new Vector3(0, 0);
             if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
             {
                 return true;
             }
             Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
             if (Game.transform.position.x == NewPos.x && Game.transform.position.y == NewPos.y)
             {
                 return true;
             }
                 
 
         }
         return false;
     }


For some reason that i cant figure out it sometimes does not work

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

2 Replies

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

Answer by Pekira · Jun 12, 2017 at 09:10 AM

The problem was when a float point imprecision happen the code would say broths vectors where different even that the difference as just 0.0000001 so i add the follow code

             if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
             {
                 return true;
             }

This Codes uses a Mathf function that compares 2 floats and return if they are similar if yes it will return true saying that a vector already exists
So basically this is what the code looks like

 bool CheackGameObjectPos(Vector3 Pos)
 {
     GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
     foreach (GameObject Game in Array)
     {
         Vector3 ZeroPos = Vector3.zero;
         if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
         {
             return true;
         }
         Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
         if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
         {
             return true;
         }
     }
     return false;
 }

@ShadyProductions thank you a lot for your answer but the code was a little bit difficult to understand

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 ShadyProductions · Jun 11, 2017 at 09:17 AM

Please note that a position contains float values, so the position needs to be exact. If you pass in a vector that has x on 0.34 and you are comparing to one that has position 0.35 then it will return false.

I also simplified it a little into LINQ statements.

         bool CheckGameObjectPosition(Vector3 position)
         {
             //Check the zero position first
             var zeroPosition = Vector3.zero;
             if (position.x == zeroPosition.x && position.y == zeroPosition.y)
             {
                 return true;
             }
 
             //Get all tile objects and select their position
             var positions = GameObject.FindGameObjectsWithTag("Tiles")
                 .Select(f => f.transform.position);
 
             //If there is any of the positions equal to our given position
             if (positions.Any(f => f.x == position.x && f.y == position.y))
             {
                 return true;
             }
 
             //Else we return false
             return false;
         }
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 Pekira · Jun 11, 2017 at 10:44 AM 0
Share

The Problem is the float error for example it is 0.7 and it transforms to 0.6999999

alt text alt text

captura-de-ecra-15.png (155.3 kB)
captura-de-ecra-16.png (158.0 kB)
avatar image ShadyProductions Pekira · Jun 11, 2017 at 01:11 PM 0
Share

Then there is nothing wrong with the function? You can additionally round of the positions

 Vector3 roundedPos = new Vector3($$anonymous$$ath.Round(position.x, 1), $$anonymous$$ath.Round(position.y, 1), 0);
 //the 1 in the math.round means 1 decimal place

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i instantiate a Vector3[] ?? 2 Answers

Can't figure class property 0 Answers

Distribute terrain in zones 3 Answers

Vector3.MoveTowards doesnt work 1 Answer

Vector3.distance is always returning 0 4 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