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 } 
  • one
    Look carefully at your animator. There are suspicions that you have transitions between states are configured to as in the lesson. Or the states do not loop, and upon execution they immediately go to another state - vmchar
  • Animations are configured exactly the same, checked several times. In the lesson, the control was implemented through the keyboard keys, I decided to remake and implement through the UI buttons, but I ran into an animation problem. - Kolhoznik
  • @Kolhoznik, i.e. Is the problem only when using EventTrigger? With direct interception of Input from the code, is everything normal? - M. Green
  • Yes, in the lesson the code was slightly different. For example, there was an update method, where animations were assigned depending on keystrokes. And in a converted version, I removed it because I do not know how to register. Added the original version of the script to the question from the lesson too - Kolhoznik
  • First: look again at the transition between the animations, it happens that you put the transition animation on equal terms everywhere, and in one place it is more or less and therefore two animations start and twitch. - MikeL

0