- Home /
Duplicate Question
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?
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()
I've fixed this issue adding this line: if (h == 0.0) rigidbody2D.velocity = new Vector2(0.0f, rigidbody2D.velocity.y);
@roberbu great answer been looking for the answer for this for days
Didn't know about the Input.GetAxisRaw() method. Thank you!
Thank goodness. InputGetAxisRaw() saved me from inertia. Now I can program $$anonymous$$egaman X's movement tightly. THAN$$anonymous$$S!