- Home /
Can we do Animation for Game objects (child or parent) using control on Buttons like Play, Pause, Reverse or Forward in Unity 5 by C# script ?
@gowtham_innvoreality
Hi I want to animate my Game objects that may be child or parent where by using button animation are controlled just like we use to watch video in player same function like Play, Pause, Reverse & Forward like that.
I was able to animate my Game object til Play, Pause & Forward animation on Game objects. But not possible on Reverse of animation.
My code below:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class cubeanima : MonoBehaviour { // Cube Parent as Game object
public GameObject Cubeanim; // Cube Parent
public Canvas myCanvas; // Canvas with Button as game object
// Use this for initialization
public void Start ()
{
Debug.Log ("Game Object Activated");
Cubeanim.SetActive (true); // Cube parent is activated
myCanvas.gameObject.SetActive(true); // Canavas is active
}
// Pause & Resume Gameobject during Game play
public void Pause_Anim()
{
Debug.Log ("Pressed Pause");
if (Time.timeScale == 0.25F)
{
Time.timeScale = 0F;
}
else
{
Cubeanim.SetActive (true);
myCanvas.gameObject.SetActive (true);
Cubeanim.GetComponent<Animator> ().enabled = true;
Time.timeScale = 0.25F;
}
}
// Repositoning Cube to original position
public void Rewind()
{
Debug.Log ("Pressed Repos");
//Unhiding all button which are game objects
Cubeanim.SetActive (false);
}
// Fastforward Cube movement
public void Forward()
{
Debug.Log ("Pressed Forward");
Cubeanim.GetComponent<Animator> ().enabled = true;
if (Time.timeScale == 0.25F)
{
Time.timeScale = 1.5F;
}
else
{
Time.timeScale = 0.25F;
}
}
}
In the above Rewind(). i have made the Game object to false instead i want game object to be true & display reverse play of Animation.
i have Animation clip of "Cmove" for my Gameobject.
Please help me on reverse. Any suggestion or advice on code are welcome.
Please tell any solution only on C#. I don't know Javascript in Unity.
Thank you.
Answer by f-Schmitz · Oct 14, 2015 at 01:27 PM
you can set the animation["animationName"].speed to -1. Then the animation should play backwards. For that you need the animation Component of your Cubeanim.
When you want to play the animation normal again. Just set the speed back to 1.
hi @f Schmitz So forward play i need to set speed to +1 & for reverse i need to set -1 !
hi @f Schmitz i'm getting error while compiling called speed not found. Whats the reason of error ?
Dont post comments as asnwers. Use the 'Add Comment' link or the little Reply arrow (This isnt immediately obvious, right of the username).
Thanks for an information.
Do you have any idea on an issue above on discussion? You are welcome Thanks
Are you trying to use the command on the GameObject?
for your code you need something like:
public Animation CubeAnim; // The animation component of your GameObject
public void Rewind()
{
[... your code...]
CubeAnim["AnimationName"].speed = -1.0f; // AnimationName is the name of the actual animation
}
It compiles for me. How have you implemented it?
Also Time.timeScale slowsdown/fastforwards your entire game, not just the animation.
@ f Schmitz
So inorder to achieve animation of forward & reverse, how can we do? By not using Time.timescale.
do I need to create different clip or single animation clip is enough ?
Your answer
Follow this Question
Related Questions
GameObject as a toggle button to play character Animation 1 Answer
Help touch button 1 Answer
Button animation state stays Pressed or Highlighted after disabling GameObject 0 Answers
Two Animations With One Button Press 1 Answer
Animator.Update High CPU Usage on Unity 5 resulting in bugs on Android 0 Answers