Hey there!
I’ve got some questions regarding the Mecanim-Animation System.
So this is my basic setup:
I’ve got my character on the left and a Sprite (2D Toolkit) on the right. It’s got a Trigger on it, when the character enters the trigger the Tree-Sprite is going to fall down to the right.
I accomplish that with an animation of the tree.
The animator has two States:
- Idle
- FallDown
While in Idle nothing happens. A Script attached to the Sprite-Objects checks if the Trigger is entered, and if so a bool variable “pushed” in the Animator is set to true, and if this condition is met Animator changes it’s state from Idle to FallDown and in FallDown the Animation is played.
Here’s the setup:
It starts in Idle and does nothing till the bool “pushed” is set to true via script.
When the condition is met, the animation is played:
(the animation is only played once, it’s looping because this is a .gif file )
Since the animation is not set to loop, the animation isn’t looping, but the state itself is of course:
This is working great. I just want to achieve something specific. I want the level to “memorize” that the tree falled down, meaning when the tree falls down, I want to save it’s position and rotation.
When the animation is finished, I save it’s position/rotation. In the Awake() method I’m checking if the variables exist, and then I load them, so on level-load the tree is automatically set to it’s fallen position/rotation.
And that’s the part where the problem comes in: it kind of works.
The thing is, when the animation is finished it does save the new position/rotation, but on level load it doesn’t load the new position but the old initial one. And this happens because the Animator-State is set again to Idle.
It only works when I disable the animator when checking for the save-file.
I’m using the Easy Save 2 Plugin to save the Transform btw.
Here’s the script I’m using:
public class PushTree : MonoBehaviour
{
public Animator anim;
public Transform treeTransform;
void Awake()
{
if(ES2.Exists("tree"))
{
Debug.Log("Tree loaded");
anim.enabled = false;
ES2.Load<Transform>("tree", treeTransform);
}
else
{
Debug.Log("Nothing to load!");
}
}
void Start()
{
StartCoroutine(SaveState());
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "PlayerHitBox")
{
anim.SetBool("pushed", true);
}
}
IEnumerator SaveState()
{
while(true)
{
if(anim.GetBool("pushed"))
{
yield return new WaitForSeconds(3.0f);
ES2.Save(treeTransform, "tree");
Debug.Log("saved");
break;
}
yield return null;
}
}
}
So I would like to know what I have to do with my states to have the same result without disabling my animator?