I'm learning how to make 2D games in unity through a tutorial, I'm in the animation part its just if statements checking player's velocity and do the right animation acording to that, however even when the player is idle, the y and x velocitys are changing randomly, i know floats arent accurate but the velocity isnt even close to 0
i searched everywhere to an answer and i haven't got anything
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
Rigidbody2D rb;
SpriteRenderer sp;
Animator anim;
[SerializeField] float jumpForce = 20f;
[SerializeField] float moveForce = 15f;
float dirX;
private enum MovementState
{
Idle, Running, Falling, Jumping
}
MovementState state;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sp = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
dirX = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(dirX * moveForce, rb.velocity.y);
UpdateAnamation();
}
private void UpdateAnamation()
{
if (dirX > 0f)
{
state = MovementState.Running;
sp.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.Running;
sp.flipX = true;
}
else
{
state = MovementState.Idle;
}
anim.SetInteger("state", (int)state);
}
}