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 DissonanceMask · Jul 08, 2020 at 05:28 PM · rigidbody2dvelocityfloat

RigidBody velocity ends with weird floating value, when axis key is released.

In the image provided, you can see that the velocity of the rigidbody turns negative for a few frames then down to zero. I would like to know why it does such, and how it can be avoided.

 ![using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     private Rigidbody2D myRB;
     private Animator myAnim;
     
     private float axisX;
     
     [SerializeField][Range(0.1f, 10.0f)] private float moveSpeed = 5.0f;
     void Start()
     {
         myRB = GetComponent<Rigidbody2D>(); myAnim = GetComponent<Animator>();
     }
 
     void Update()
     {
         Debug.Log(myRB.velocity.x);
         
         axisX = Input.GetAxis("Horizontal");
         FlipSprite();
     }
 
     private void FixedUpdate()
     {
         Run();
     }
 
     private void Run()
     {
         Vector2 playerVelocity = new Vector2(axisX * moveSpeed, myRB.velocity.y);
         myRB.velocity = playerVelocity;
     }
 
     private void FlipSprite()
     {
         bool hasHorizontalSpeed = Mathf.Abs(myRB.velocity.x) > Mathf.Epsilon;
         if (hasHorizontalSpeed)
         {
             transform.localScale = new Vector2(Mathf.Sign(myRB.velocity.x), 1f);
         }
     }
 }][1]
 


[1]: /storage/temp/162996-tilevania.png

tilevania.png (245.3 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Jul 08, 2020 at 09:21 PM

Hey there,


the weird value you got there is something like -1.5E-14. So -1.5 devided by (10^14). which is 0. At least in the world of floating point precision. When handling floats you can never be 100% certain that if you calculate 100/10 it will actually be 10 in the end. The result might also be 9.999999999.... or 10.000000000001. There are certain imprecisions that can not be avoided in floating point mathematics. Thus it can also lead to "negative" numbers.


That said - how do you avoid that? -> You don't. There is just no way to ensure that this does not happen.

Comment
Add comment · 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
0

Answer by Ronako · Feb 07 at 12:59 AM

For anyone still having issues with this and happens across this article, here is my solution to this weird problem. By creating a new variable lastInputValue, and setting it equal to moveInput.x when moveInput is not zero (can probably do this by checking moveInput.x != 0, or the way I have shown, with moveInput != Vector2.zero), we can get rid of any floating point inaccuracies by using this lastInputValue inside our FlipSprite.

     float lastInputValue = 1f;
 
     void OnMove(InputValue value)
     {
         moveInput = value.Get<Vector2>();
         if (moveInput != Vector2.zero)
         {
             lastInputValue = moveInput.x;
         }
     }
 
     void OnJump(InputValue value)
     {
         if (value.isPressed && collider.IsTouchingLayers(LayerMask.GetMask("Ground")))
         {
             rigidBody.velocity += new Vector2(0f, jumpSpeed * Time.fixedDeltaTime);
         }
     }
 
     void FlipSprite()
     {
         playerHasHorizontalSpeed = Mathf.Abs(rigidBody.velocity.x) > Mathf.Epsilon; // Epsilon is technically better to use than 0 for comparing.
 
         if (playerHasHorizontalSpeed)
         {
             transform.localScale = new Vector2(Mathf.Sign(lastInputValue), 1f);
         }
     }


Good luck everyone!

Comment
Add comment · 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
0

Answer by Ronelo · Feb 07 at 10:39 AM

Try to Change GetAxis to GetAxisRaw .

Comment
Add comment · 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

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

137 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

Related Questions

Keeping momentum with rigid bodies. 0 Answers

Rigidbody2D with interpolation lags while moving by velocity, how to solve the problem? 1 Answer

Issue with direction and rigibody2D 1 Answer

Do rigidbodies (especially 2d) have limits on their velocities? 1 Answer

Diagonal jump results with weird motion. 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