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 vipvex · May 19, 2012 at 05:33 PM · playermovefix

Mathf.SmoothStep not quite working.

I'm working on a script that smoothly moves my character (that has a rigid body) to a selected position. But i need that character to be exactly on the position for example: 1, 0, 1. But my script doesn't do that. Instead it moves him to 1, 0, 0.99999999. I think that it doesn't work because it's a rigid body and reacts with the terrain and moves it a tiny bit. For my other scripts to work i need the player to be at an exact int. I looked into some Mathf functions but none of them worked. Here's my script: function FixedUpdate () { if (turnCompleting){ if (NextTurnMove){ //while (!transform.position.x == nextMovingPosition.x && !transform.position.z == nextMovingPosition.z){ rigidbody.position.x = Mathf.SmoothStep(curTransform.position.x, nextMovingPosition.x, .3); rigidbody.position.z = Mathf.SmoothStep(curTransform.position.z, nextMovingPosition.z, .3); //rigidbody.MovePosition(Vector3.Slerp(curTransform.position, nextMovingPosition, .3)); //} if (curTransform.position.x == nextMovingPosition.x && curTransform.position.z == nextMovingPosition.z){ turnCompleting = false; } } } }

Comment
Add comment · Show 1
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 Reavenk · May 25, 2012 at 04:39 AM 0
Share

Sound more like signs of floating point errors. $$anonymous$$aybe you could post-process the float value so that "if $$anonymous$$athf.Abs(value % 1.0f) < 0.0001f" (if it's really close to an integer), the value is then forced to be "value = $$anonymous$$athf.Round(value)" (force the value to that close integer)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · May 25, 2012 at 07:31 AM

For my other scripts to work i need the player to be at an exact int.

Important point: Unity does not represent position with integers. Like most modern game engines, it uses floating point numbers -- and those numbers have limited precision.

If you expect exact equality between floats, you're probably going to run into trouble. Most people get into the habit of instead checking for floats which are "approximately equal". You can use Unity's `Mathf.Approximate()` helper function, or write your own.

Consider: if we assume that one Unity unit is one meter, your character is at most ten nanometers away from the position you're expecting. Is that really something you need to worry about?

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

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

Multiple Cars not working 1 Answer

Making a block move when its clicked 1 Answer

Line Renderer Collision Detection. 2 Answers

2D Movement Problems 2 Answers

How can i loop my path instead of it ending on the lasat point 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