Skip to content

Instantly share code, notes, and snippets.

@MuhammadFaizanKhan
Created May 9, 2020 07:06
Show Gist options
  • Save MuhammadFaizanKhan/4b2e4d9551687e4c43b909f93349a87a to your computer and use it in GitHub Desktop.
Save MuhammadFaizanKhan/4b2e4d9551687e4c43b909f93349a87a to your computer and use it in GitHub Desktop.
Play Unity legacy animation on click, click again to play animation in reverse. click and toggle animation
using UnityEngine;
public class AnimationController : MonoBehaviour
{
[SerializeField]
Animation anim;
[SerializeField]
string clipName;
private float animationSpeed = 0.5f;
private void Reset()
{
anim = GetComponent<Animation>();
clipName = anim.name.ToString();
}
bool isForwad = true;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//playing forwad animation
if (isForwad)
{
animationSpeed = 0.5f;
anim.Play();
anim[clipName].speed = animationSpeed;
}
else
{
//if the Animation backward mode and time is zero then animation has finsihed.
if (anim[clipName].time == 0) {
anim[clipName].time = anim[clipName].length;
}
animationSpeed = -0.5f;
anim.Play();
anim[clipName].speed = animationSpeed;
}
isForwad = !isForwad;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment