Home>

In Unity, I made a 3d animation where a ball jumps and falls and lands.

using System.Collections.Generic;
using UnityEngine;
public class Animechange: MonoBehaviour {
    Animator anim;
    private Rigidbody rb;
    private string state;// State management
    private string prevState;// previous state save
    [SerializeField]
    Raycast raycast;
    [SerializeField]
    private float key;// Management of input

    void Start ()
    {
        // retrieve each
        anim = GetComponent  ();

    }
    void Update ()
    {
        GetInputKey ();// Get input
        ChangeState ();// State transition
        ChangeAnimation ();// Change animation according to state
    }
    private void GetInputKey ()
    {
        key = 0;
        key = Input.GetAxisRaw ("Horizontal") + Input.GetAxisRaw ("Vertical");

    }
    private void ChangeState ()
    {
        // I'm on the ground
        if (raycast.hit.collider.gameObject.tag == "Floor")
        {
            //running
            if (key! = 0)
            {
                state = "MOVE";
            }
            // landing
            else if (prevState == "JUMP")
            {
                state = "LANDING";
            }
            // stop
            else if (key == 0)
            {
                state = "IDLE";
            }

        }
        // When in the air
        else if (raycast.hit.collider.gameObject.tag == "Untagged")
        {
                state = "JUMP";
        }

    }
    private void ChangeAnimation ()
    {
        // Do it only when the state transitions
        if (prevState! = state)
        {
            switch (state)
            {
                case "JUMP":
                    anim.SetBool ("isJump", true);
                    anim.SetBool ("isLanding", false);
                    anim.SetBool ("isMove", false);
                    anim.SetBool ("isIdle", false);

                    break;
                case "LANDING":
                    anim.SetBool ("isJump", false);
                    anim.SetBool ("isLanding", true);
                    anim.SetBool ("isMove", false);
                    anim.SetBool ("isIdle", false);
                    break;
                case "MOVE":
                    anim.SetBool ("isJump", false);
                    anim.SetBool ("isLanding", false);
                    anim.SetBool ("isMove", true);
                    anim.SetBool ("isIdle", false);

                    transform.localScale = new Vector3 (Mathf.Abs (gameObject.transform.localScale.x) * key, gameObject.transform.localScale.y, gameObject.transform.localScale.z);

                    break;

                default:
                    anim.SetBool ("isJump", false);
                    anim.SetBool ("isLanding", false);
                    anim.SetBool ("isMove", false);
                    anim.SetBool ("isIdle", true);
                    break;
            }
            // Save state
            prevState = state;
            Debug.Log (prevState);
        }
    }
}
Code
using System.Collections;
public class Raycast: MonoBehaviour
{
    public RaycastHit hit;
    [SerializeField]
    bool isEnable = false;
    void OnDrawGizmos ()
    {
        if (isEnable == false)
            return;// Noactive
        var scale = transform.lossyScale.x * 0.5f;
        // Hit
        var isHit = Physics.BoxCast (transform.position, Vector3.one * scale, Vector3.down, out hit, transform.rotation);
        // Debug
        if (isHit)
        {
            Gizmos.DrawRay (transform.position, Vector3.down * hit.distance);
            Gizmos.DrawWireCube (transform.position + Vector3.down * hit.distance, Vector3.one * scale * 2);
        }
        else
        {
            Gizmos.DrawRay (transform.position, Vector3.down);
        }
    }
}
Code


I wrote this, but when prevState is JUMP, going to LANDING doesn't work, and even if I land, it's still in the jumping animation. What should I do?