How do you make an animation reverse? I read somewhere you could just reverse the speed, but I don’t really know how to access the animation’s speed, adjusting the animator speed doesn’t seem to do anything… I have this script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorOpen : MonoBehaviour
{
List<BimbuStats> bimbi = new List<BimbuStats>();
public Animator anim;
private void Start()
{
bimbi.Add(GameObject.FindObjectOfType<BimbuStats>());
anim = GetComponentInParent<Animator>();
}
private void Update()
{
foreach(var bimbus in bimbi)
{
if (Vector3.Distance(transform.position, bimbus.transform.position) < 10f)
{
anim.speed = 1;
}
else
{
anim.speed = -1;
}
}
print(bimbi.Count);
}
}
It’s basically just a door, and I want the door to open when one of the creatures gets close to it, but close when any one is far enough away. I can get it to open, and then instantly slam shut by setting a bool, but I’d like it to close gradually.
You can reverse animations, it just requires a weird work around lol, here’s how I did it. I just recently used this in a project in 2020.3.1f1 so it should work. No need to create a second animation.
I tried this workaround and it wouldn’t play the animation state from the end of the clip.
I figured out a new workaround, just adding an animation component and populating it with my animation clips. This forces me to set clips as legacy to add them, and then disable legacy to work with the timeline, which is fine … but I am trying to set them back to legacy from my customeditor. Sometimes this works, sometimes not …
animation = GetComponent<Animation>();
List<AnimationState> states = new List<AnimationState>();
foreach (AnimationState state in animation)
{
states.Add(state);
}
animation doesn’t always populate my animation states. I know they need to be non legacy, which is the state they are in when I run the code above, but like I said, sometimes they are not available in animation.
Sorry for the late response, I’ve never really tried adding animations to a list from an animator, but why are you running your foreach loop for the animation and not the animator? Why are you even getting a reference to your animations? Not judging if that’s what you want to do, just genuinely curious lol
Edit: also this should work just fine in 2020 and 2019. I think the issue is you’re getting a reference and setting the speed of the animation directly, instead of the animator. Sorry, I just realized I never specified that my “anim” was the animator, not animation.
Because I am no longer adding an Animator Component, just an Animation Component.
I was trying to access the each animation state to enable legacy so they play at run time due to having to have legacy disabled to create the animations in the timeline. It was working at first but then began to more times than not return a null array. So, because I am just doing this all in editor I switched to just updating the legacy setting to the clips directly in the directory.
I tried the conventional method of updating the custom parameter, but the animation would never play from the end.
the last method did nothing, although I feel like it used to, and the first method (your suggestion) I had tried and it would play the animation (viewable in the animator), but never from the end, so it looks as if it was not animating.
Either way, the method I have implemented works great … was a little extra work on the initial back end … but that’s all done now and the front end works all the same. Well, almost, one added step on the front end of either toggling the legacy off to be able adjust the timeline or legacy on to play the animation on run.