I never animated in the Unity engine before. Mainly I made games with just shapes but I tried out sprite animation but when the animation plays it locks my player in place and it breaks game physics so my player cant fall. I tried removing the empty game object that checks if it’s jumping but it still doesn’t work. Here is the code please let me know what I’m doing wrong thank you.
private Rigidbody2D rb;
public float runSpeed;
public float jumpForce;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float jumpTimeCounter;
public float jumpTime;
public bool isJumping;
private Animator anim;
void Start() {
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate() {
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * runSpeed, rb.velocity.y);
}
void Update() {
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.W)) {
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if(moveInput > 0) {
transform.eulerAngles = new Vector3(0, 0, 0);
} else if (moveInput < 0) {
transform.eulerAngles = new Vector3(0, 180, 0);
}
if (moveInput < 0) {
anim.SetBool("isRunning", false);
}
else {
anim.SetBool("isRunning", true);
}
if (Input.GetKey(KeyCode.W) && isJumping == true) {
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.W)) {
isJumping = false;
}
}
}