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 /
This question was closed Mar 06, 2014 at 08:26 AM by Fattie for the following reason:

Duplicate Question

avatar image
3
Question by Via Cognita · Mar 06, 2014 at 06:18 AM · 2dgravityinertia

Remove movement inertia

I'm learning Unity development with the tutorial 2D Game Development Walkthrough. I'm adapting its scripts to my development and I have a problem: when I stop pressing left or right key cursor, my hero still moves a little until he stops.

This is the script I'm using:

 public class PlayerControl : MonoBehaviour
 {
     [HideInInspector]
     public bool facingRight = true;            // For determining which way the player is currently facing.
 
     public float moveForce = 365f;            // Amount of force added to move the player left and right.
     public float maxSpeed = 5f;                // The fastest the player can travel in the x axis.
 
     private Animator anim;                    // Reference to the player's animator component.
 
     void Awake()
     {
         // Setting up references.
         anim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     void FixedUpdate()
     {
         // Cache the horizontal input.
         float h = Input.GetAxis("Horizontal");
 
         // The Speed animator parameter is set to the absolute value of the horizontal input.
         anim.SetFloat("Speed", Mathf.Abs(h));
 
         // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
         if (h * rigidbody2D.velocity.x < maxSpeed)
             // ... add a force to the player.
             rigidbody2D.AddForce(Vector2.right * h * moveForce);
 
         // If the player's horizontal velocity is greater than the maxSpeed...
         if (Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
             // ... set the player's velocity to the maxSpeed in the x axis.
             rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
 
         // If the input is moving the player right and the player is facing left...
         if (h > 0 && !facingRight)
             // ... flip the player.
             Flip();
         // Otherwise if the input is moving the player left and the player is facing right...
         else if (h < 0 && facingRight)
             // ... flip the player.
             Flip();
     }
 
     void Flip()
     {
         // Switch the way the player is labelled as facing.
         facingRight = !facingRight;
 
         // Multiply the player's x local scale by -1.
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

My hero has a RigidBody 2D with the same properties as in the 2D Tower Defence sample.

I would like to have gravity, but I don't like to have inertia or maybe a little inertia.

How can I remove this inertia?

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

  • Sort: 
avatar image
9
Best Answer

Answer by robertbu · Mar 06, 2014 at 06:27 AM

There are two possible issues here. The first is that you are using Rigidbody2D.AddForce(). As with real world objects, your objects will have momentum. You can fix this problem in a couple of ways. First try upping the Linear and Angular Drag settings in the Rigidbody2D component. If you want absolute control, set the velocity directly rather than using AddForce(). When the "Horizontal" axis is 0.0 for example, set the x of the rigidbody2d velocity to 0.0.

The second issue is with Input.GetAxis(). This function smooths the values a bit. Replace them with Input.GetAxisRaw()

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 Via Cognita · Mar 06, 2014 at 06:41 AM 0
Share

I've fixed this issue adding this line: if (h == 0.0) rigidbody2D.velocity = new Vector2(0.0f, rigidbody2D.velocity.y);

avatar image stevehealy · Mar 01, 2017 at 10:18 AM 0
Share

@roberbu great answer been looking for the answer for this for days

avatar image Damjan-Mozetic · Aug 17, 2017 at 02:16 PM 0
Share

Didn't know about the Input.GetAxisRaw() method. Thank you!

avatar image meiousei · Dec 17, 2018 at 12:46 AM 0
Share

Thank goodness. InputGetAxisRaw() saved me from inertia. Now I can program $$anonymous$$egaman X's movement tightly. THAN$$anonymous$$S!

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Change the "down" axis for 2D 2 Answers

2D Top Down Character being Pulled down and movement script not working? 2 Answers

How can I make a object get bigger as it falls? 1 Answer

Why is there no gravitational acceleration with Unity 4.3.1 2D? 0 Answers

How do i make my character rotate when falling? 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