- Home /
Reverse animation problem
Hii
I am working on reverse animation but some conditions are not satisfied to work as i wanted.
I am having one scale animation for x axis. Now i have empty state as default.
Next state is scale animation .
Transition between this two state is having one boolean parameter that is trigger.
On collisionenter i am making this "trigger" parameter to true so my animation will work.
Now i want this animation to work as reverse also so i have added one float parameter as "speed" which is set to 1.
and i have added this parameter as multiplier to speed.
Once simple scale animation is completed i want to start timer for 5 seconds and after 5 seconds i want same animation in reverse.
Code is here :
using UnityEngine;
using System.Collections;
public class moveFlower : MonoBehaviour {
float timer = 0.0f;
bool timercheck = false;
static float timeRemaining = 5.0f;
GameObject plank;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.down * Time.deltaTime * 0.5f , Space.World);
if(timercheck)
{
StartCoroutine("countDownTimer");
}
// if(this.transform.position.y < -5.0f)
// {
// Destroy(this.gameObject);
// }
}
void OnCollisionEnter(Collision obj)
{
if(obj.gameObject.tag.Equals("platform"))
{
plank = obj.gameObject;
obj.gameObject.GetComponent<Animator>().SetBool("trigger" , true);
plank.gameObject.GetComponent<Animator>().SetFloat("speed" , 1.0f);
this.gameObject.GetComponent<MeshRenderer>().enabled = false;
timercheck = true;
}
}
void countDownTimer()
{
if(timeRemaining < 0)
{
plank.gameObject.GetComponent<Animator>().SetBool("trigger" , true);
plank.gameObject.GetComponent<Animator>().SetFloat("speed" , -1.0f);
timercheck = false;
}
Debug.Log ("Time :" + timeRemaining);
timeRemaining -= Time.deltaTime;
}
}
But next time scale animation is not working. Hope all you get idea,.
This script is attached to flower object and flower prefab are instantiated randomly.
Thanks.
Answer by Priyanshu · Aug 21, 2015 at 09:18 AM
One way is to make another Animation State. Assign it Scale animation. Set its speed to -1.
So basically there are 3 states :
Deault State (No animation)
Scale Up State (Scale animation), Speed = 1 and Non-looping.
Scale Down State (Scale Animation), Speed = -1 and Non-looping.
Inside OnCollisionEnter method, set transition from Default State to Scale Up State.
Inside CoundownTimer Coroutine, set transition from Scale Up State to Scale Down State
Assign exit time of 3rd transtion i.e. from Scale Down State to Default State to 1. This transitions animation to default state when Scale Down animation completes.
Ok but how to identify that scale down is completed ? can you please post some code ?
How to set exit time ?
After scale down completes. Animation will automatically transit to default state due to exit time.
But if u want to identify enter/exit points off animation states. For that u will need Stat$$anonymous$$achineBehaviour scripts.
In Animator Window click on the transition arrow and in the Inspector you will see "Has Exit Time" bool
Your answer
Follow this Question
Related Questions
Button animator not playing. 0 Answers
Animator is not playing an AnimatorController 1 Answer
root motion on in place animation 0 Answers
Dynamic way to adjust animated sprite length 0 Answers
Check Animator State Animation Completed 0 Answers