Currently, I am having a Cube that needs to be animated to roll on its edge. Using unity built-in animation feature, I created 4 animations (RotateUp
, RotateDown
, RotateLeft
, RotateRight
). At the end of each animation I would put an Event to call a method on my script. To be able to move the object every time the animation is played, I put the cube with animator attached inside a parent object.
Below is the script attached to my cube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RedCubeBehavior : MonoBehaviour
{
Animator anim;
bool isMoveTriggered = false;
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
MoveUp();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
MoveDown();
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
MoveLeft();
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
MoveRight();
}
if (isMoveTriggered)
{
if (IsBackToIdle())
{
transform.parent.position = transform.position;
transform.localPosition = Vector3.zero;
isMoveTriggered = false;
}
}
}
void AnimationFinished()
{
anim.Play("IdleState");
}
public void MoveUp()
{
if (isMoveTriggered)
{
return;
}
else
{
Debug.Log("MoveUp");
isMoveTriggered = true;
anim.Play("RotateUp");
}
}
public void MoveDown()
{
if (isMoveTriggered)
{
return;
}
else
{
Debug.Log("MoveDown");
isMoveTriggered = true;
anim.Play("RotateDown");
}
}
public void MoveLeft()
{
if (isMoveTriggered)
{
return;
}
else
{
Debug.Log("MoveLeft");
isMoveTriggered = true;
anim.Play("RotateLeft");
}
}
public void MoveRight()
{
if (isMoveTriggered)
{
return;
}
else
{
Debug.Log("MoveRight");
isMoveTriggered = true;
anim.Play("RotateRight");
}
}
bool IsBackToIdle()
{
return anim.GetCurrentAnimatorStateInfo(0).IsName("IdleState");
}
}
Every time the arrow key is pressed the object would rotate but in the end when AnimationFinished()
is called, the cube would just roll back to its starting position (because of playing IdleState
). It will only work (the object didn't return to its starting position) if I pressed the arrow twice quickly.
I've tried to not call the IdleState
(which is only an empty state), but what happens next is it just won't play another animation again when triggered.
What should I do in order to get it work without having to press the key twice?