Animator script need help. Character flying animation while in the air not working

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class character : MonoBehaviour
{
    public float speed = 6.0f;
    public float rotateSpeed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    private Animator anim;
    private Vector3 moveDirection = Vector3.zero;
    private CharacterController controller;
    private int jumps;
   
    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    
   
    }
    // Update is called once per frame
    void Update()
    {
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if(Input.GetButtonDown("Jump"))
            {
                moveDirection.y = jumpSpeed;
              
            }
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Jumping");
            anim.SetBool("IamJumping", true);
        }
    
      
        
      
    }
}

I have an problem i want my character to kind of fly so you can jump high and you almost have no gravity and then we can shoot each other with fireballs and the ground is lava with some cubes you can stand on.
Maybe later when im better at coding i can make the character fly with a cooldown and more abilities you can pick up.
But i have one problem in the code when the character asset: devil animated character ( is free btw )
is in the air i want it to play the flying animation so when i press jump it plays it but that is also the problem
because it only plays it 1 second how do i make the animation play when my character is in the air? Is this even possible …

check if the “Loop Time” option is marked in the import settings of your model.

It is maybe i didn’t explain it well the animation only plays when i PRESS the jump button i want the animation to play when my character is in the air "flying¨

if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Jumping");
            anim.SetBool("IamJumping", true);
        }

Thats my animation code

Actually, that animation code explains nothing about your problem. This is just activation instruction, but animation import settings and animator transitions configuration is what important in this case. You need to make sure your animation is set to loop time and the exiting transitions for that flying animation state are not triggered while in air.

1 Like