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 Ochreous · May 27, 2015 at 11:35 PM · c#transformpositionfloatround

C# round transform.position floats to .5

I would like to round my gameobjects transform.position floats to .5 when I move. I was able to get it to work with Mathf.RoundToInt but with Mathf.Round/2 the gameobject never makes it to 0.5 resets back to 0 and then locks up. What am I doing wrong with my code?

             Vector3 movePositions = new Vector3(Mathf.Round(transform.position.x + Input.GetAxis("Horizontal"))/2,Mathf.Round(transform.position.y + Input.GetAxis("Vertical"))/2,transform.position.z);
             transform.position = Vector3.Lerp(transform.position, movePositions, Time.smoothDeltaTime);
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 cjdev · May 27, 2015 at 11:56 PM 0
Share

If you're doing this in an Update function it may be that your Input.GetAxis isn't getting a large enough value to step up the transform position high enough before getting rounded off every frame.

2 Replies

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

Answer by nesis · May 27, 2015 at 11:46 PM

Hi, I think you're wanting Mathf.Round(). It still rounds to the nearest while number, but doesn't return an integer.

To round how you're wanting, I'd make a function like this :

 public static float RoundToNearestHalf(float a)
 {
     return a = Mathf.Round(a * 2f) * 0.5f;
 }

Any float you pass into that function will be rounded to the nearest 0.5 :)

Another thing to note, you'll want to at least occasionally check and correct the actual values in transform.position, to ensure they're still rounded to 0.5. Depending how you move that transform, you will get consecutive rounding errors, which can quickly add up and result in inaccurate placement of your object.

If you're dealing with VERY large values, this function might be a safer option:

 public static float RoundToNearestHalfSafer(float a)
 {
     return a = a - (a % 0.5f);
 }

This version uses the modulus operator to get the remainder of dividing "a" by 0.5, then subtracts that from "a" to get a rounded value.

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 kreso · Nov 26, 2018 at 02:26 PM 1
Share

Thank you, this quick code helped me do what I need. For completion;

 public static float SnapTo(float a, float snap)
  {
      return $$anonymous$$athf.Round(a / snap) * snap;
  }

avatar image triangle4studios · Apr 26, 2021 at 08:19 PM 0
Share

This doesn't take negative numbers into account.

avatar image
0

Answer by triangle4studios · Apr 26, 2021 at 08:11 PM

Or simply:

 public static float Half(float value)
 {
     return Floor(value) + 0.5f;
 }
 // Standalone Floor Function, Not necessary, but good for beginners to understand how it works.
 public static float Floor(float value)
 {
     return value > 0 ? (value - value % 1) : (value - (value % -1));
 }
   

 
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

23 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

Related Questions

Why are floats not accessing my inputted values? 1 Answer

why is transform.position giving me local location?(c#) 0 Answers

Floating-point errors while assigning transforms 1 Answer

An Efficient Way To Follow Transform's Position? 3 Answers

Lerp to where the target was x seconds ago? 0 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