© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
Chazher

✅ when i'm grounded my running animation won't trigger

-

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


public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private Animator anim;
private SpriteRenderer sprite;
[SerializeField] private LayerMask jumpableGround;
private BoxCollider2D coll;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float JumpFoce = 15f;

private enum MovementState {idle, running, jumping, falling }

private float dirX = 0f;

// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}

// Update is called once per frame
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new UnityEngine.Vector2(dirX * moveSpeed, rb.velocity.y);

if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new UnityEngine.Vector2(rb.velocity.x, JumpFoce);
}
UpdateAnimationState();

}

private void UpdateAnimationState()
{
MovementState state;

if (dirX > 0f)
{
state = MovementState.running;
sprite.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.running;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}

if (rb.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}

anim.SetInteger("state", (int)state);
}

private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f,jumpableGround);
}
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Route Doesn't Trigger When Called Externally
C#CC# / help
15mo ago
❔ Why can't I place my animation onmy score script?
C#CC# / help
3y ago
✅ Wpf app won’t load when published
C#CC# / help
13mo ago
My code isn't running as anticipated
C#CC# / help
4y ago