In a video tutorial, I made an animation of a 2D character and I try to apply these animations during character movements. Movement is done via the UI button through EventTrigger and the events PointerDown and PointerUP.
Created the enumeration as indicated in the lesson, where the animations themselves go and try to apply them when the character moves. But there are problems with the animation when the character moves. For example, when a player goes to the right (the ForwardButtonDown method is executed) at first the player seems to be animating as needed i.e. Run animation is on, but after a few movements itβs as if he is jumping, although I continue to press the button to the right (forward for a 2D character).
Here is a complete script .. Please tell me how to properly implement the animation in the code
public class CharacterScript : MonoBehaviour { [SerializeField] private int lives = 5; //ΠΊΠΎΠ»-Π²ΠΎ ΠΆΠΈΠ·Π½Π΅ΠΉ [SerializeField] private float speedX = 1.0F; //Π‘ΠΊΠΎΡΠΎΡΡΡ ΠΈΠ³ΡΠΎΠΊΠ° [SerializeField] private float jumpForce = 15.0F; [SerializeField] private float horizontalSpeed; new private Rigidbody2D rigidbody; private Animator animator; private SpriteRenderer sprite; private bool isGrounded; private PlayerState State { get { return (PlayerState) animator.GetInteger("State"); } set { animator.SetInteger("State", (int) value); } } private void Awake() { rigidbody = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); sprite = GetComponentInChildren<SpriteRenderer>(); } private void Start() { speedX = 0; } public void BackButtonDown() { speedX = -horizontalSpeed; sprite.flipX = true; if (isGrounded) { State = PlayerState.Run; } } public void ForwardButtonDown() { speedX = horizontalSpeed; sprite.flipX = false; if (isGrounded) { State = PlayerState.Run; } } public void BackOrForwardButtonUp() { speedX = 0; State = PlayerState.Idle; } public void UpButton() { if (isGrounded) { rigidbody.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } } private void FixedUpdate() { transform.Translate(speedX, 0, 0); CheckGround(); } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { isGrounded = true; State = PlayerState.Idle; } } private void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.tag == "Ground") { isGrounded = false; State = PlayerState.Jump; } } private void CheckGround() { Collider2D[] collides = Physics2D.OverlapCircleAll(transform.position, 0.3F); isGrounded = collides.Length > 1; if (!isGrounded) { State = PlayerState.Jump; } } } public enum PlayerState { Idle, Run, Jump } Original version of the lesson
public class Character : Unit { [SerializeField] private int lives = 5; //ΠΊΠΎΠ»-Π²ΠΎ ΠΆΠΈΠ·Π½Π΅ΠΉ [SerializeField] private float speed = 3.0F; //Π‘ΠΊΠΎΡΠΎΡΡΡ ΠΈΠ³ΡΠΎΠΊΠ° [SerializeField] private float jumpForce = 15.0F; //ΡΠΈΠ»Π° ΠΏΡΡΠΆΠΊΠ° new private Rigidbody2D rigidbody; private Animator animator; private SpriteRenderer sprite; private bool isGrounded = false; private PlayerState2 State { get { return (PlayerState2) animator.GetInteger("State"); } set { animator.SetInteger("State", (int) value); } } private void Awake() { rigidbody = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); sprite = GetComponentInChildren<SpriteRenderer>(); } private void FixedUpdate() { CheckGround(); } void Update () { if (isGrounded) { State = PlayerState2.Idle; } if (Input.GetButton("Horizontal")) { Run(); } if (isGrounded && Input.GetButtonDown("Vertical")) { Jump(); } } public void Run() { //Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ Π±Π΅Π³Π° Vector3 direction = transform.right * Input.GetAxis("Horizontal"); transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime); sprite.flipX = direction.x < 0.0F; if (isGrounded) { State = PlayerState2.Run; } } public void Jump() { rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); } private void CheckGround() { Collider2D[] collides = Physics2D.OverlapCircleAll(transform.position, 0.3F); isGrounded = collides.Length > 1; if (!isGrounded) { State = PlayerState2.Jump; } } } public enum PlayerState2 { Idle, Run, Jump }