Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Invertex · Feb 11, 2014 at 08:20 AM · physicsvelocityphysics2dnormalized

.normalized sometimes returns 0 values, in a condition that only functions if value is not 0...

I'm having a bit of an issue with this very simple bit of code. I'll show it first:

     if(rigidbody2D.velocity.x != 0)
     {
         Debug.Log ("Velocity Direction: " + new Vector2(rigidbody2D.velocity.x,0).normalized.x);
     }

As you can see, if my X velocity is higher/lower than 0, then we log the normalized direction of that velocity. Yet when I'm turning back and forth, resulting in a very slow X velocity, or turning on a slope, it will result in some 0 values, despite it only triggering if my .X velocity is not 0... (and this isn't an issue with the if statement I don't think, as it doesn't log anything if I stand still)

Does .normalized just round down the value if it's super close to 0? And if so, what do you think I could use instead to get around this issue?

Comment
Add comment · Show 2
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 robertbu · Feb 11, 2014 at 08:29 AM 0
Share

I'm guessing it is the Debug.Log() that is rounding or truncating the value. $$anonymous$$aybe you want to use a threshold:

 if ($$anonymous$$athf.Abs(rigidbody2D.velocity.x) > someValue) { 
avatar image Invertex · Feb 11, 2014 at 09:10 AM 0
Share

Unfortunately that doesn't solve the problem, it would seem it's an issue with the .normalized and not the condition. =/

1 Reply

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

Answer by DajBuzi · Feb 11, 2014 at 08:47 AM

Your Vector is toomsmall to be normalized. As it's stated in documentation:

 "If the vector is too small to be normalized a zero vector will be returned."
Comment
Add comment · Show 4 · 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 Invertex · Feb 11, 2014 at 08:55 AM 0
Share

Dang =/ Guess I'll just have to do a workaround by adding an condition in my code to only update the float if the normalized value is also not 0. Unless there's another operation that I could use which would work similar to normalized... Thanks.

avatar image DajBuzi · Feb 11, 2014 at 09:07 AM 0
Share

If you want to just set this value between 0.0 and 1.0 then Robertbu gaved you solution, but if you want set ot from -1.0 to 1.0 then... i'm guessing that you should use $$anonymous$$athf.Clamp <- it's only a guess :p

avatar image Invertex · Feb 11, 2014 at 09:13 AM 0
Share

Nah, I need it to either be -1 if it's below 0, or 1 if above 0. Robert's solution would still allow for that, as it's only changing the condition, not the result. The issue, as you mentioned, arises from Normalized not handling that low of values.

Clamp would also only handle one direction. I think I'll just resort to using an if/else ins$$anonymous$$d, I don't need such micro-optimization anyway. So if the value is below, I'll just feed a -1 to the variable, and vice versa.

 if(rigidbody2D.velocity.x > 0)
 {
 moveDir = 1;
 }
 else if(rigidbody2D.velocity.x < 0)
 {
 moveDir = -1;
 }
avatar image Invertex · Feb 11, 2014 at 11:21 AM 0
Share

After thinking on it a bit, I thought of a solution for the normalize value issue. I use the normalized value in my IF condition as well.

 if(new Vector2(rigidbody2D.velocity.x,0).normalized.x != 0)
 { 
 Debug.Log ("Velocity Direction: " + new Vector2(rigidbody2D.velocity.x,0).normalized.x);
 }

And we're good to go. That way it knows if I'm going to be fed a 0 value by normalized and thus won't execute. Though I'll probably stick with the if/elseif, as it's likely more efficient than calculating the normalized value haha.

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

19 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

Related Questions

Predict next rigidbody position based on velocity 1 Answer

Vector3.normalize not behaving as expected? 1 Answer

how to get starting velocity of an object, which is thrown with a 45 degree angle and will pass some (x,y) point 0 Answers

Need help with my movement script.. :( 1 Answer

Is there a way to lock velocity? 3 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