- Home /
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.
?? 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?
@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.
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.
sorry guys, there were mistakes in the preceding code. I commented below under the answer.
Answer by meat5000 · Jul 17, 2015 at 09:56 AM
You cant.
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.
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.
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?
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220611231712im_/https://answers.unity.com/themes/thub/images/avi.jpg)