This has been affecting the run animation because the jump animation will start the play while the player runs.
Here is a video, look at the console to see the y velocity: https://www.loom.com/share/3880603a5d4e4963a2e3664ea5642361
Look in the comments for my player movement and animation script.
#Y Velocity increases when my player is running.
1 messages · Page 1 of 1 (latest)
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private int speed = 20;
private float jumpStrength = 30;
private float horiInput;
private bool isFacingRight = true;
[SerializeField] Rigidbody2D rigid;
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundLayer;
[SerializeField] Animator animation;
// Update is called once per frame
private void Update()
{
Debug.Log(rigid.velocity.y);
horiInput = Input.GetAxis("Horizontal");
flip();
// Jump button
if (Input.GetKeyDown(KeyCode.Space) == true && isGrounded())
{
rigid.velocity = new Vector2(rigid.velocity.x, jumpStrength);
}
// Decreases jump height if player lets go early
if (Input.GetKeyUp(KeyCode.Space))
{
rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * 0.5f);
}
// sets running animation
if (horiInput != 0)
{
animation.SetBool("isRunning", true);
}
else
{
animation.SetBool("isRunning", false);
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
// Sets jumping animation
if (rigid.velocity.y > 0)
{
animation.SetBool("IsJumpingUp", true);
}
else
{
animation.SetBool("IsJumpingUp", false);
}
// Sets falling animation
if (rigid.velocity.y < 0)
{
animation.SetBool("IsJumpingDown", true);
}
else
{
animation.SetBool("IsJumpingDown", false);
}
}
// Moves the player left and right
private void FixedUpdate()
{
rigid.velocity = new Vector2(speed * horiInput, rigid.velocity.y);
}
// Checks if player is grounded
private bool isGrounded()
{
return Physics2D.OverlapCircle(groundCheck.transform.position, 0.2f, groundLayer);
}
// Flips player if they are looking and moving in opposite ways
private void flip()
{
if ((isFacingRight && horiInput < 0) || (!isFacingRight && horiInput > 0))
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x = localScale.x * -1f;
transform.localScale = localScale;
}
}
}
Ayy that worked thanks