I have 4 animations
idle
running
jump
running jump
Idle is the default animation
There are 2 boolean parameters, ismoving and isjumping. The ground boolean is just a test so it won't affect anything
along with this code
public class Player_Movement : MonoBehaviour
{
public CharacterController controller;
public Transform camera;
public float gravity = -9.81f;
public bool isMoving;
public float speed = 12f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float jumpHeight;
public Vector3 fallDown;
public bool jumpAble = true;
public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("ArrowKeysHorizontal");
float z = Input.GetAxis("ArrowKeysVertical");
Vector3 direction = new Vector3(x, 0f, z).normalized;
isMoving = direction.magnitude >= 0.1 ? true : false;
if (isMoving)
{
animator.SetBool("IsMoving", true);
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + camera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDirection.normalized * speed * Time.deltaTime);
}
else
{
animator.SetBool("IsMoving", false);
}
if (controller.isGrounded)
{
animator.SetBool("Grounded", true);
animator.SetBool("IsJumping", false);
fallDown.y = -2f;
if (Input.GetKey(KeyCode.Space) && jumpAble)
{
animator.SetBool("Grounded", false);
jumpAble = false;
StartCoroutine(JumpCoolDown());
animator.SetBool("IsJumping", true);
fallDown.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
fallDown.y += gravity * Time.deltaTime;
controller.Move(fallDown * Time.deltaTime);
}
public IEnumerator JumpCoolDown()
{
yield return new WaitForSeconds(1.3f);
jumpAble = true;
}
if the player is moving, it transitions to the run animation
if it ismoving is false and isjumping is true, it jumps
if ismoving and isjumping are true, it running jumps
I have a few problems that I need help with: I can't run while running, and if I jump, and then move midair and land, I keep moving but the character is stuck in the last frame of the jump animation (jumping bool will not turn to false even though I'm grounded).
Why is this happening and how can I fix it?
↧