Player animations start too late and when the character stops moving it slides slowly until it actually stops.

When I try to move, the player starts moving with the idle animation then, after a second it starts the running animation, and when I stop moving (when I do not hold the key) it slides slowly before it actually stops. That’s how it looks link text

And this is the player script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    public float maxspeed = 3;
    public float speed = 50f;
    public float jumppower = 150f;
    float size = 0.2f;
    public bool grounded;
    private Rigidbody2D rb2d;
    private Animator anim;


void Start () {
    //player movement
    rb2d = gameObject.GetComponent<Rigidbody2D>();
    anim = gameObject.GetComponent<Animator>();
}

void Update()
{

    anim.SetBool("Grounded", grounded);
    anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
    if (Input.GetAxis("Horizontal") < -0.1f)
    {
        transform.localScale = new Vector3(-size , size , size);
    }
    if (Input.GetAxis("Horizontal") > 0.1f)
    {
        transform.localScale = new Vector3(size, size, size);

    }

}
void FixedUpdate()
{

    float h = Input.GetAxis("Horizontal");
    //limiting the speed
    if (rb2d.velocity.x > maxspeed)
        rb2d.velocity = new Vector2(maxspeed, rb2d.velocity.y);

    rb2d.AddForce((Vector2.right * speed) * h);

    if (rb2d.velocity.x < -maxspeed)
    {
        rb2d.velocity = new Vector2(-maxspeed, rb2d.velocity.y);
    }

}
}

And this is the “ground check” script.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GroundCheck : MonoBehaviour{
    private Player player;

    void Start()
    {
        player = gameObject.GetComponentInParent<Player>();
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        player.grounded = true;
    }

    void OnTriggerExit2D(Collider2D col)
    {
        player.grounded = false;
    } }

You’re moving your character adding a force to the rigidbody. This implies the character will accelerate and deccelerate depending on the impulse force.

I suggest you change directly the rigidbody velocity in the X axis, or the rigidbody position:

void FixedUpdate()
{
    float h = Input.GetAxis("Horizontal");

    //Changing velocity directly
    rb2d.velocity = new Vector2(h * maxspeed, rb2d.velocity.y);

    //Changing position directly
    Vector2 newPos = rb2d.position;
    newPos.x += h * maxspeed * Time.deltaTime;
    rb2d.position = newPos;
}

Choose whatever you like the most.

For the animation delay, it could be fixed by adjusting the Animator transition time between idle and run animations, but I’m not sure as I can’t see it :slight_smile: