• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 Megaman X's movement tightly. THANKS!

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

24 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

Related Questions

Question on 2d planet gravity 1 Answer

Jump and run on a circle (2D) 0 Answers

How can I make a gravity field similar to a planetary body? 2 Answers

Gravity switch 1 Answer

Change the "down" axis for 2D 2 Answers

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