• 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 LBandy · Aug 13, 2012 at 04:09 PM · vector3charactercontrollermovefloatprecision

CharacterController.Move precision

I give CharacterController.Move() the exact some input, however the result positions differ from time to time. The difference is somet$$anonymous$$ng like 0.0000010, but in the long term, it grows huge.

Any ideas, where can I find the roots of t$$anonymous$$s problem?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Aug 13, 2012 at 04:10 PM

Sure, here ;)

Comment
Add comment · Show 7 · 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 LBandy · Aug 13, 2012 at 07:08 PM 0
Share

Well, thank you for pointing out the issues about float precision, but I'm already aware of that. I've managed to make the input every time the same using rounding and casting, however what I figured out, is that the only I thing I can not change and Unity handles it incorrectly, is the position. As I experienced, positions are only stored in x.xx format, so the basic problem is, that after the third point, the numbers are lost.

So the question is basically the following: how can I manage to get the CC to the same position after the same cc.Move() command using the same input? What should I cast, round, etc... I can not believe, that there isn't a solution to this kind of problem.

avatar image Loius · Aug 13, 2012 at 07:38 PM 0
Share

Why do you need such precise movement?

The only way to reach the level of precision it sounds like you're asking for is to cast your floats to integers and work from there.

avatar image LBandy · Aug 13, 2012 at 07:46 PM 0
Share

I'm working on a multiplayer game, where there are two players, and their positions sets the limits where the other can move. So if I'm out of sync for like 0.0000001 game unity, then in the long run they will be totally messed. Syncing their positions is not an option, so I'm catching the whole input and reproducing the whole movement on each machine, and not getting to the same position is not so good.

I was thinking about integers as well, but I must convert them to floats to use the cc.Move function (I need to detect collision) so I can not avoid working with float as long as Move only accept floats as an input. Until this point, everything is under control and works as expected.

avatar image Bunny83 · Aug 14, 2012 at 02:23 AM 0
Share

Can you post the code you're using locally and the one you use remote? to move your character? How do you send the input?

The position IS stored as 3 normal float values. That means the bigger the number gets the smaller the precision gets behind the floating point.

If you do the same on two machines it "should" produce the same result if the conditions are 100% the same. There are cases where floating point units from different brands had a different behaviour in certain cases, but usually if it follows the "normal single-floats" (IEEE 754) standard it should be exactly the same. Are you sure that you don't have a time difference or framerate dependencies?

avatar image LBandy · Aug 14, 2012 at 05:38 AM 0
Share

I run every movement calculation in FixedUpdate. What I do right now is, fill up an array with the input from FixedUpdate as well (-1,0,1, no interpolation between them), and send the content of the array periodically in a single string via a local call (for my player locally) and via RPC (for my player on the network) in the same time. I use the exact same script to work with the input transmitted. I break down the string, and get a single input per fixedupdate frame. I do the calculations then do a single .Move() (only one per fixed frame). After this I move on to the next input.

The array submitted is exactly the same, I checked it several times, if I sum up the input and the float values used by the .Move() method periodically is also the exact same. So the script goes wrong after making the actual movement, and needed to calculate where we are.

Right now I've rounded down every float to 3 point precision to match the vector. This way, I'm getting much closer values, but still not identical.

Here is a bit of code to demonstrate what I am talking about:

newPos.x = input runSpeed 0.02f;


lastIn = (float)newPos.x;


controller.Move(new Vector3(lastIn, newPos.y, 0));


lastMove = (float)(transform.position.x - lastX);


if(lastIn != lastMove)


{


Debug.Log("Input: " + lastIn + ", Moved: " + lastMove);
}


lastX = transform.position.x;

And the output is:


Input: 0.06, Moved: 0.06000018


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.06000018


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.06000018


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.06000018


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.06000042


Input: 0.06, Moved: 0.05999994


Input: 0.06, Moved: 0.06000042


However, if the controllers position is between 0 and 1 on the x axis, the output is almost equal.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Vector3 using Doubles Instead of Floats 2 Answers

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

maximum distance from origin (float precision) 1 Answer

Precision problem when moving Rect values 2 Answers

CharacterController Gravity 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