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?
-
Answer # 1
Related articles
- c # - about controlling unity animation
- c # - i have a question about unity unitychan
- c # - about unity score addition and display
- about c # and unity of dot installation
- c # - about gravity with unity
- c # - [unity] about the type of function that returns multiple types
- c # - i want to play animation to unity music
- c # - about unity hinge joint script
- c # - about unity ball launch
- [unity, android development] about soft keyboard preview
- c # - [unity] format exception: input string was not in a correct format error
- c # - [wpf] how to wait for the animation to finish playing and start another process
- c # - unity 2d action i want to make the enemy blow away when attacking
- c # - i want to rotate an object such as picturebox with animation
- c # - i have a question about setting up opengl processing for ios in xamarin
- c # - about the operation of xamarin prismpluginpopups
- c # - how do i turn on setactive in unity?
- about conditional expressions in c # while statement
- c # - i added the arjs plugin to unity, but webar does not appear in the browser
- c # - i want to create a webar in unity
Related questions
- c # - [unity] i am changing the value of a public variable from another script, but the value seen from the script with that pub
- c # - i want to replace a file with the same name in asset
- c # - error "nullreferenceexception: object reference not set to an instance of an object"
- c # - create a new scene with a script
- c # - i made a unity oculus rift laser pointer, but it doesn't respond
- c # - compile error when importing oculus integration in unity 20194
- c # - vroid in unity
- i want to use addforce to move forward when i press the q key in a c # script
- c # - play sound effects using unity is trigger
- c # - i want to create a webar in unity
It seems that processing was heavy.