Home>

I am currently making a 3D action game with Unity and C #. I am using CharacterController to move the character, but at the start of the jump I use CharacterController's isGround = true so I don't know what to do after the jump.
(Jump is not played if anim.SetBool ("jump", false) is written in the same isGround while jumping with if (isGround) of InputButton in CharacterAnimation script)
Enable characters to jump in Unity I have read this article.
If i know how to do this, you can cut the animation halfway.
Thank you.

using System.Collections.Generic;
using UnityEngine;
public class DS4InputManager: MonoBehaviour {
    protected float Rx, Ry, Lx, Ly = 0;
    protected virtual void Update ()
    {
            InputJoysticks ();
        if (Input.anyKeyDown)
        {
            InputButton ();
        }
    }
    protected virtual void InputJoysticks ()
    {
        Rx = Input.GetAxisRaw ("Horizontal");
        Ry = Input.GetAxisRaw ("Vertical");
        Lx = Input.GetAxis ("LHorizontal");
        Ly = Input.GetAxis ("LVertical");
        Debug.Log ("Right (" + (int) Rx + "," + (int) Ry + ")");
        Debug.Log ("Left (" + (int) Lx + "," + (int) Ly + ")");
    }
    protected virtual void InputButton ()
    {
        if (Input.GetButtonDown ("DS4x"))
        {
            Debug.Log ("Pressed x (space)");
        }
        if (Input.GetButtonDown ("DS4o"))
        {
            Debug.Log ("Pressed o (enter)");
        }
        // Add below if there is an addition
        // if (Input.GetButtonDown (""))
        // {
        //}
    }
}
using System.Collections.Generic;
using UnityEngine;
public class CharacterActionManager: DS4InputManager {
    private CharacterController C_controller;
    [SerializeField]
    private float movespeed;
    [SerializeField]
    private float jumppower;
    private CharacterAnimation C_Animation;
    private Vector2 direction;
    private Vector2 jumpdirection;
    private void Start ()
    {
        C_controller = gameObject.GetComponent<CharacterController>();
        C_Animation = gameObject.GetComponent<CharacterAnimation>();
    }
    protected override void Update ()
    {
        base.Update ();
        GroundedCheck ();
        CharacterMove ();}
    protected override void InputJoysticks ()
    {
        base.InputJoysticks ();
        Debug.Log (Lx);
        if (Lx! = 0)
        {
            CharacterdirectionChange (Lx);
            Lx = Lx * movespeed;
            direction = new Vector2 (Lx, 0);
        }
        else
        {
            direction = new Vector2 (0, 0);
        }
    }
    protected override void InputButton ()
    {
        base.InputButton ();
        if (C_controller.isGrounded)
        {
            if (Input.GetButtonDown ("DS4x"))
            {
                jumpdirection.y = jumppower;
            }
        }

    }
    private void CharacterMove ()
    {
        jumpdirection.y-= jumppower * 2 * Time.deltaTime;
        direction = direction + jumpdirection;
        C_controller.Move (direction * Time.deltaTime);
    }
    private void GroundedCheck ()
    {
        if (! C_controller.isGrounded)
        {
            Debug.DrawLine (gameObject.transform.position, gameObject.transform.position-gameObject.transform.up, Color.green);
            if (Physics.Linecast (gameObject.transform.position, gameObject.transform.position-gameObject.transform.up))
            {
                C_Animation.isGround = true;
            }
            else
            {
                C_Animation.isGround = false;
            }
        }
        if (C_controller.isGrounded)
        {
            C_Animation.isGround = true;
        }
    }

    private void CharacterdirectionChange (double direction)
    {
        Vector3 Angle = gameObject.transform.localEulerAngles;
        if (direction == 0) return;
        if (direction>0)
        {
            Angle.y = 90f;
        }
        if (direction<0){
            Angle.y = -90f;
        }
        gameObject.transform.localEulerAngles = Angle;
    }

}
using System.Collections.Generic;
using UnityEngine;
public class CharacterAnimation: DS4InputManager
{
    private Animator anim;
    private bool cananim;
    private bool stop;
    public bool isGround;
    public bool landground;

    private void Start ()
    {
        anim = gameObject.GetComponent  ();
        cananim = false;
        stop = false;
        isGround = false;
        landground = true;
    }
    protected override void InputJoysticks ()
    {
        base.InputJoysticks ();
        if (Lx! = 0)
        {
            if (! anim.GetCurrentAnimatorStateInfo (0) .IsName ("jump"))
            {
                anim.SetBool ("run", true);
            }
        }
        else
        {
            anim.SetBool ("run", false);
        }
    }
    protected override void InputButton ()
    {
        base.InputButton ();
        if (! stop)
        {
            if (Input.GetButtonDown ("DS4o"))
            {
                anim.SetTrigger ("skill" + 1);
                Debug.Log ("skill" + 1);
            }
        }
        if (isGround)
        {
            if (Input.GetButtonDown ("DS4x"))
            {
                anim.SetBool ("jump", true);
                Debug.Log ("JUMP");
            }
            anim.SetBool ("jump", false);
        }

    }
}