Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 devstudents · Jul 17, 2015 at 09:46 AM · positionvector3grid

How to make a Vector3 A = Vector3 B EXACTLY

I just want to know if this is even possible. I used the following code to change the player's position to the position of a tile in my 3D grid based project:

 Vector3 temp = playerPathScript.target; 
 temp.x = Mathf.RoundToInt(playerPathScript.target.x);
 temp.y = 0; 
 temp.z = Mathf.RoundToInt(playerPathScript.target.z); 
 playerPathScript.player.position = temp; 

The problem is that player is slightly off. For example instead of being 16, 0, 24, the player is 15.999889, 0.156, 23.999889. The most confusing is the y position as I have no idea why that is greater than zero. I'm trying to get the player's position to be exactly the same as the grid tile's position.

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 Owen-Reynolds · Jul 17, 2015 at 12:24 PM 2
Share

?? player.position = playerPathScript.target will make them exactly the same. The code you wrote clearly makes player and target different ... you really want to do what?

You say the player is at x=15.999889 AFTER this code? That's not possible. RoundToInt returns an exact 16.0f. Something else is moving the object a tiny bit. Does it have a ridigbody? Some other code moves it?

avatar image tanoshimi · Jul 17, 2015 at 12:50 PM 0
Share

@mattockman Please show the rest of the code up to the point where you are calling the Debug.Log to print the values 15.999889, 0.156, 23.999889.

avatar image Bonfire-Boy · Jul 17, 2015 at 01:41 PM 0
Share

Yeah, the OP needs a Debug.Log of playerPathScript.player.position immediately after line 5 as shown above, to convince themselves of whether it's this code that's making y=0.156 or something else.

$$anonymous$$inor correction to Owen's comment... RoundToInt returns an int.

avatar image devstudents · Jul 17, 2015 at 10:25 PM 0
Share

sorry guys, there were mistakes in the preceding code. I commented below under the answer.

1 Reply

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

Answer by meat5000 · Jul 17, 2015 at 09:56 AM

You cant.

https://www.google.co.uk/search?q=floating+point+error&ie=utf-8&oe=utf-8&client=ubuntu&channel=fs&gfe_rd=cr&ei=K9GoVY3BJ5P98wfNrKS4Bw&gws_rd=ssl

You can make 2 variables point to the same piece of information but I doubt very highly that that bit of information will ever be so accurate. This is the only way to make them equal. This wont make them accurate.

In JS for example, I read that an int is just a parsed float anyway.

I've used Matlab and such programs extensively and it will produce accurate reproductions just about every time. Unfortunately Unity's floating point is not nearly so accurate as it simply does not need to be.

Where an integer might tell you its a round number, place that into a transform and its a float. With error.

Use RountToInt and store it in a float. With Error.

The further you get from the origin, the larger the error gets also.

I think a Unity float is accurate to .000001 (I could be wrong I worked it out last week). Which isnt great to be honest.

The point is, I guess, making Unity swear to you that two numbers are the same is nice to make you feel good but by the time it uses the value there will be error introduced.

This will always happen in floats.

Make note of Daves point that an error of 0.156 from an int is NOT FLOATING POINT ERROR and something else is at work.

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 Dave-Carlile · Jul 17, 2015 at 12:40 PM 0
Share

While this is true, he's setting temp.y to 0, and it's displaying as 0.156 which is well beyond a precision issue. Looks like something else is going on.

avatar image Bonfire-Boy · Jul 17, 2015 at 01:53 PM 0
Share

Is it really true? That you can't make one float the same as another (and hence one vector3 the same as another)?

Regardless of precision errors, surely setting the bits of one float to be the same as those of another makes them the same?

avatar image Owen-Reynolds · Jul 17, 2015 at 03:01 PM 0
Share

Bobfire Boy: if you read those search results, they never say you can't make one float the same as another. Of course you can. The computer can copy bits w/o errors.

The only problem with floats has to do with these two things: 1) (obviously) infinite/repeating decimals need to be rounded, 2) since the computer stores fractions in binary, lots of numbers are repeating.

The easy example is 0.1. The computer has to make it out of sixteens, thirty-seconds ... . It repeats. What happens is, someone sees 0.1 times 10 doesn't equal one, so has to think the computer has some inexplicable extra error. But once you realize 0.1 is a rounded binary fraction, it makes perfect sense.

avatar image Dave-Carlile · Jul 17, 2015 at 04:22 PM 1
Share

I think we're kind of talking about two different things here. Yes, if you set x = y, x will have the exact same value as y. But that may not be the exact value you think it is.

 x = 0.00001f;
 y = x;

 if (x == y) print 'true';


That's going to print true every time. But if you actually display the value of x and/or y, it might show something like 0.0000099999999.

It isn't necessarily equal to the exact value you set it to.

avatar image devstudents · Jul 17, 2015 at 10:24 PM 1
Share

Well, this is somewhat embarrassing, however I'm still happy I posted as I've gleaned some interesting information about floats that I didn't know before.

The code leading up to that block was faulty. When I corrected that, the player position did equal the target position (i.e., the node in this case) perfectly. This was a brute force method. As you can see by the code, it literally changes the player's position to those values. It doesn't move the player there or do anything the requires complex or even simple float calculations. When trying to make the player's position equal to the node position through movement or some other method, it was always imprecise (the y was faulty for some other reason that I haven't deciphered yet).

I've taken from these comments that the only way to make two vector3 values the same is to literally tell unity: this Vector3 = that Vector3. Trying to do the same thing through a movement method etc, will always lead to slightly imprecise results.

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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Transform angle to grid position 2 Answers

Instantiating objects at position 1 Answer

C# Transform modification. 1 Answer

How to stop transform movement at a certain point? 1 Answer

Any help with prevent camera from passing a specific coordinate? 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