Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by hobocampus · Oct 13, 2015 at 01:11 AM · transformlocalscale

Unwanted Decimal Value When Scaling Object

 var player : Transform;
 
 function OnMouseOver ()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         transform.localScale += new Vector3(0F, .1F, 0);
     }    
     
     if(Input.GetButton("Fire2") && transform.localScale.y > .1f)
     {
         transform.localScale += new Vector3(0F, -.1F, 0);
     }
 }

I'm using this script to increase scale on object by .1 when I left click and decrease scale by .1 when I right click. When the object's scale gets above 2.7, it goes to 2.799999 instead of 2.8. Why is this happening and is there a way to prevent it from doing so?

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

Answer by dkjunior · Oct 13, 2015 at 03:33 AM

This is an inherent problem of floating-point operations. In most cases it shouldn't affect the logic/behavior. It does, however, creates problems when you need to compare two floating-point values (as 2.8 won't match 2.799999). Standard approach for these situations is to check whether two values are very close to each other (as opposed to checking for exact equality):

 float a = 2.799999;
 float b = 2.8;
 
 bool equal = Mathf.Abs(a - b) < 1e-3f;
Comment
Add comment · Show 1 · 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 hobocampus · Oct 13, 2015 at 10:47 PM 0
Share

And you answered my next question, too. Thanks!

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

32 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

Related Questions

I can not modify localscale while I'm playing because of the Animator. 1 Answer

Changing transform.localscale doesn't take affect immediately? The next RayCast isn't precise 1 Answer

In multiplayer: how can I have unique transform scalings for each player that show for every play but only affect the individual? 0 Answers

function works script-wise; doesn't work visually(only in game) 0 Answers

How to multiply gameobject size on collision?,How to increase the size of a gameObject as a multiple using a script for unity 3d? 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