The question is answered, right answer was accepted
Can't jump while running, and animation won't transition
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?
I fixed the not being able to jump while running by adding the if (Input.GetKey(KeyCode.Space)) part into the moving section, but I now have the same, stuck in the last frame problem as the normal jump. Also, I don't understand why that worked and the previous version didn't. Why did the first version not work? I was grounded, pressing the key, and was jumpable so why didn't the if statement run?
Also, the if statement for jumping, under the moving if statement won't run if I add controller.IsGrounded to the if statement, even though I put an if statement before it that returned a message saying the character was grounded.
Follow this Question
Related Questions
Custom Animation Editor 0 Answers
Animator displaying wrong states 1 Answer
[2D] Play animation backward. 1 Answer
Stop unity adding a prefix on import. 0 Answers
Animation import issue 0 Answers