Flipping Animation/Jumping animation
Hello there UnityAnswers.
I have been struggling with a project of mine, and I hope you guys can give me an insight.
I have this small sprite character, with a sold movement script. I've created a Flipping method to flip him when facing left, and i repert this method when he turns right again. But the thing is: the flipping doesn't happen when I press two movement keys (A or D) at once. I would like him to go towards the last pressed key, but even if the character itself goes sideawys, the animation does not. What should I do?
Also I have this sweet jumping animation, with a perfect take off and a landing timing, but the thing is: when my character is running, and ends up jumping, the running animation continues playing, instead of the JumpStarting animation. What should I do?
I am including my script and some video examples of my issues. Any other kind of feedback wil be highly appreciated. Thank You!
[RequireComponent(typeof(Rigidbody2D))]
public class Player_script : MonoBehaviour
{
[SerializeField]
private float _speed = 10f;
[SerializeField]
private float _jumpForce = 10f;
Rigidbody2D rb;
public bool _canJump = false;
private Animator _anim;
private bool _facingRight;
// Start is called before the first frame update
void Start()
{
_facingRight = true;
rb = GetComponent<Rigidbody2D>();
_anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Movement();
Jumping();
}
private void Movement()
{
float horizontal = Input.GetAxis("Horizontal");
transform.Translate(Vector2.right * _speed * horizontal * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
Flip(horizontal);
_anim.SetBool("IsRunning?", true);
if(Input.GetKeyDown(KeyCode.Space) && _canJump == true)
{
_anim.SetBool("IsRunning?", false);
_anim.SetBool("JumpStart", true);
}
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
Flip(horizontal);
_anim.SetBool("IsRunning?", true);
if (Input.GetKeyDown(KeyCode.Space) && _canJump == true)
{
_anim.SetBool("IsRunning?", false);
_anim.SetBool("JumpStart", true);
}
}
if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
{
_anim.SetBool("IsRunning?", false);
}
else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
{
_anim.SetBool("IsRunning?", false);
}
}
private void Jumping()
{
if(Input.GetKeyDown(KeyCode.Space) && _canJump == true)
{
_anim.SetBool("JumpStart", true);
rb.AddForce(Vector2.up * _jumpForce, ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.CompareTag("Ground"))
{
_canJump = true;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
_anim.SetBool("JumpEnding", true);
_anim.SetBool("JumpStart", false);
_canJump = false;
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !_facingRight || horizontal < 0 && _facingRight)
{
_facingRight = !_facingRight;
Vector2 Scale = transform.localScale;
Scale.x *= -1;
transform.localScale = Scale;
}
}
}
Here the gif image with the example: https://imgur.com/a/ty7HLu7
Your answer
Follow this Question
Related Questions
Problem With Character Animations 0 Answers
skip a bit forward in current animation? 0 Answers
2D Spine Animation not playing 0 Answers
Animation flipped in wrong direction? 1 Answer
How do I get my animated Object to switch animations on Collider? 1 Answer