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 EBR · May 24, 2014 at 08:45 PM · mathfincrementround

Round to 0.5, 1.5, 2.5 ...

Is there a way to round a number in increments of one but to be offset by one? So for example if a number is 0.7 it would round down to 0.5 and if a number is 1.2 it will round up 1.5. Would it use Mathf.Round or is this impossible? Thanks

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
3
Best Answer

Answer by chillersanim · May 24, 2014 at 08:47 PM

Use:

 float roundedValue = ((int)input) + 0.5f;

That should do it.

Edit:
If you want to use negative numbers, then use this instead:

 float output = Mathf.Sign(input) * (Mathf.Abs((int)input) + 0.5f);

Greetings
Chillersanim

Comment
Add comment · Show 6 · 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 EBR · May 24, 2014 at 08:50 PM 0
Share

Thank you, I feel so stupid now. :P

avatar image EBR · May 24, 2014 at 09:06 PM 0
Share

hmmm... after some testing this shows some weird results... I can't seem to figure out why.

void OnDrawGizmos() { Vector3 p = camera.ScreenToWorldPoint(Input.mousePosition); p = new Vector3(((int)p.x) + 0.5f, ((int)p.y) + 0.5f, 0); Gizmos.DrawCube(p, Vector3.one); }

avatar image chillersanim · May 24, 2014 at 09:24 PM 0
Share

Be aware, that the method "camera.ScreenToWorldPoint" does not fire a ray, but ins$$anonymous$$d it returns a point at the overloaded z distance.

When you want to get where the mouse points on, then you should use the ray class.

$$anonymous$$ore informations can be found in this answer:
http://answers.unity3d.com/questions/265108/shoot-ray-from-mouse-cursor.html

Greetings
Chillersanim

avatar image chillersanim · May 24, 2014 at 09:32 PM 0
Share

I added a formula for negative numbers.

When you need to work in worldspace, then it is likely that you get negative numbers, so try using the second formula ins$$anonymous$$d.

Greetings
Chillersanim

avatar image EBR · May 24, 2014 at 09:33 PM 0
Share

The only problem is I don't have any colliders set up, so the raycasting won't work for my specific setup. But I guess I will have to change that. Thanks for your help!

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Round x, y and z values of object? 1 Answer

Time.timescale=0 not working when the timeleft "countdown" reduce to 0 0 Answers

How to round or convert doubles? 2 Answers

Rounding to multiples of 0.5 1 Answer

Increment a float Value from A to B in N seconds 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