• 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
1
Question by megabrobro · Dec 15, 2017 at 05:01 PM · unity 5rigidbodyvelocityaddforce

Problem reading/getting from rb.velocity value?

Hi everyone, I am trying to give a particular object a boost of force on Y, when its own velocity if over a certain Y velocity.

So imagine a Mario 1 style game, if the mushroom was moving over the block below, and the block below nudged it upwards this will pause it to increase its velocity.y naturally. In my code I'm trying to add a significant boost to it when that happens.

Its very confusing, because I can see by my Debug.Log messages that the velocity is over the set amount, but the boost isnt occuring (NOTE: If i make the set amount for boosting very slow such as 1f or less, then every time it goes over an edge of platform it gets the boost (presumably because it gets a slight bump at the end of the platform). Here is the code, and then the Debug messages, which I think when looked at together describe the problem perfectly.

Can anybody tell me how its possible that the Debug reports the velocity.y to be 5.96... , but the then code that says if (vel.y > 5.9f) // Do this , never gets called?

(NOTE: I already tried putting this part of the code into LateUpdate() and FixedUpdate() but it seemed to have no effect)

             if (rb.velocity.y > 0 )
                 Debug.Log("vel y = " + rb.velocity.y);
             if (rb.velocity.y > 5.5f)
             {
                 Debug.Log("why doesnt it boost!" + rb.velocity.y);
                 if (!beingBoosted)
                 {
                     Debug.Log("BOOSTING NOW = " + rb.velocity.y);
                     rb.AddForce(0f, 0.7f, 0, ForceMode.Impulse); // add small boost to make powerup jump
                     beingBoosted = true;
                 }
             }


the log reads:

vel y = 5.960464E-08 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)

vel y = 1.266599E-07 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)

vel y = 5.960464E-08 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)

vel y = 1.655453 UnityEngine.Debug:Log(Object) PowerupBig:LateUpdate() (at Assets/Scripts/PowerupBig.cs:75)

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

Answer by pako · Dec 15, 2017 at 07:59 PM

@megabrobro you are not reading the log right. It doesn't say velocity.y is 5.96, it says velocity.y is 5.960464E-08, which is practically zero! i.e. 5.960464E-08 = 5.960464 x 10^(-08) (read 5.96 times 10 to the power of -8, which is the same as 5.96 divided by 100,000,000 => practically zero). So, this is why vel.y > 5.9f is false.

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 Cresspresso · Dec 15, 2017 at 08:18 PM 0
Share

You can use rb.velocity.y.ToString("0.00") to display the float value to 2 decimal places.

avatar image megabrobro · Dec 15, 2017 at 08:19 PM 0
Share

Ah thank you very much

avatar image Bunny83 · Dec 15, 2017 at 08:52 PM 1
Share

Right, it's crucial to learn Scientific notation.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

163 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 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 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

Countering AddForce by Modifying velocity 3 Answers

Stopping a rigidbody immediately 2 Answers

Gravitational pull without losing speed 1 Answer

Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers

Using rigidbody.AddForce and speeding the object 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges