- Home /
Animation on Button press, then same button plays in reverse
Hello all
I have a simple object cube, when i press KeyR it plays an animation of the cube changing scale (getting bigger), Happy days.
However when i press KeyR again, i want it the play the animation again but in reverse.
so i can grow the cube and shrink the cube just by pressing R
I have a very simple script attached that is assigned to my cube, which gives me a menu in inspector to select the Key and which transition condtion to trigger play, as per below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RiskAnimation : MonoBehaviour
{
public KeyCode MyKey;
public string RiskTrigger;
void Update()
{
if (Input.GetKey(MyKey))
{
GetComponent<Animator>().SetTrigger(RiskTrigger);
}
}
}
My animator parameters look like this, i have an Idle state with no animation attached, with a transition to my animation with a trigger
I either want to play the same animation in reverse, or play a new animation that i have created which is just a reverse of the first animation i play
i maybe using a C# wrong or not the right script, Very new to C#
and help would be greatly appreciated
Answer by LogCatGames · Feb 21, 2019 at 04:07 PM
You could use a bool instead of a trigger...
Here is a script that can probably work :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RiskAnimation : MonoBehaviour { public KeyCode MyKey; public bool Risk; public Animator animator;
void Start()
{
Animator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey(MyKey))
{
Risk =! Risk;
Animator.SetBool("PlayAnimation", Risk);
}
}
}
And in your Animator Controller make a loop between your two animations with the bool... I think you know how work bool with animator... If not call me back !
Hope I helped !
Bye
Thank you for the reply
Still struggling a little
please see attached image of the animator
the script i have like this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayAnimationRisk : $$anonymous$$onoBehaviour
{
public $$anonymous$$eyCode $$anonymous$$y$$anonymous$$ey;
public bool Risk;
public Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (Input.Get$$anonymous$$ey($$anonymous$$y$$anonymous$$ey))
{
Risk = !Risk;
animator.SetBool("Playfoward", Risk);
}
}
}
Currently noting is happening Any ideas?
Try to write
Risk =! Risk;
ins$$anonymous$$d of :
Risk = !Risk; (It will mark it as false if you write this...)
Your answer
Follow this Question
Related Questions
Using Override Animation Controllers (NOT WORKING) 0 Answers
the animation walk but he still in the same place and rotate left and right on this white circle 0 Answers
How can I change my AnimatorState instantly? 1 Answer
Animator override controller fails to play sprite animation 1 Answer
Is there a way to change the WrapMode of all Animations in a Mecanim Animator? 1 Answer