Question by
mikeycgreen100 · Nov 16, 2021 at 08:55 PM ·
unity 2d
unity 2d character stuck in jump animation
when i jump the jump animation stays in place please help me heres the heres the code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MoveAndFlip : MonoBehaviour { public Animator animator; public float MovementSpeed = 1; public float JumpForce = 1; [Header("Is your character facing right before runtime? If yes, check box")] public bool facingRight;
public float horizontalValue;
public float speed;
private Rigidbody2D _rigidbody;
// Update is called once per frame
private void Start()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
move();
// use this for the wrong way
//wrongWayToFlip();
// use this for a better way
properFlip();
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalValue));
if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
{
animator.SetBool("IsJumping", true);
_rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
}
public void OnLanding ()
{
animator.SetBool("IsJumping", false);
}
void move()
{
horizontalValue = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
//transform.Translate(new Vector3()
}
void properFlip()
{
if((horizontalValue < 0 && facingRight) || (horizontalValue > 0 && !facingRight))
{
facingRight = !facingRight;
transform.Rotate(new Vector3(0, 180, 0));
}
}
void wrongWayToFlip()
{
if ((horizontalValue < 0 && facingRight) || (horizontalValue > 0 && !facingRight))
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
Comment