• 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 miningcheeseman · Dec 15, 2017 at 02:09 AM · transform.positionrounding

How to round the coordinates of a game object to an integer or "nice" number?

I am trying to make a very basic snap to grid (and only on the X axis) but the Round function isn't quite working for me.

void Update () { Mathf.RoundToInt(transform.position.x); }

I was wondering if there's another way, or I'm just doing something wrong here?

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 JVLVince · Dec 15, 2017 at 04:05 AM 0
Share

Can you explain more "isn't quite working for you" mean? Or give more info about ur problem? AFAI$$anonymous$$ the $$anonymous$$athf.RoundToInt is acceptable in this case (I mean round case). The only thing I think I have to mention you is this special case:

If the number ends in .5 so it is halfway between two integers, one of which is even and the other odd, the even number is returned.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Dec 15, 2017 at 05:01 AM

Yes, you do something wrong here. RoundToInt is a method that returns the rounded value as integer. In your code you don't do anything with the returned value. If you want to actually move the objects to the rounded position you have to do this:

 Vector3 tmp = transform.position;
 tmp.x = Mathf.Round(tmp-x);
 transform.position = tmp;

Note that you have to use an actual temporary variable as you can't directly change only one component of transform.position as it's a property (getter and setter combo) and Vector3 is a valuetype.


Also note that i did not use RoundToInt as this would be pointless. A Vector3 consists of floating point numbers. So you could use RoundToInt but it would have to convert the returned integer back to a float anyways. So just using Round is enough.

Comment
Add comment · Show 3 · 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 JVLVince · Dec 15, 2017 at 05:15 AM 1
Share

I've learned something here. +1

avatar image miningcheeseman · Dec 16, 2017 at 03:58 AM 0
Share

Thanks! This really helped out!

avatar image Bunny83 miningcheeseman · Dec 16, 2017 at 04:25 AM 0
Share

If the question is answered you should accept the answer.

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

74 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 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 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 avatar image avatar image

Related Questions

[solved] Having a problem with respawning script. the player didn't go to the proper respawn point 1 Answer

Unable to apply "transform" on gameobject from Raycast 1 Answer

Problem with transform.position on unity 3D,Transform.position not working as expected unity3D 0 Answers

Object not moving with code 0 Answers

the player don't move with the transporter,,The occurrence of the player from the carrier 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