Created
May 9, 2020 07:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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