- Home /
How Can I get the mouse to Cycle Through Animations Each Time I click it? Please Help!
I am trying to get the mouse to play a new animation every time I click it down and when I release it go back to the idle position, How Can I Achieve this? this is as far as I got
if ( Input.GetButtonDown("Fire1"))
{
_animator.SetBool("PunchingLeftJab", true);
}
else
{
_animator.SetBool("PunchingLeftJab", false);
}
Answer by Chimer0s · Feb 08, 2019 at 12:16 PM
Assuming they're all bools in the animator, you could create a list or array of strings matching those bools, then create an int that gets added to each time you click and use it to access the index of that array/list.
string[] animations = { "anim1", "anim2", "anim3" };
int chosenAnim = 0;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
if (chosenAnim < animations.Length - 1)
chosenAnim++;
else
chosenAnim = 0;
_animator.SetBool(animations[chosenAnim], true);
}
else
{
_animator.SetBool(animations[chosenAnim], false);
}
}
Edit:
I'd also switch over to using triggers in the animator instead of bools if you're firing off one time animations that aren't meant to loop until a condition is changed.
Edit 2:
I realize you asked about cycling instead of selecting an animation at random. I've updated the code to go through them in order and reset instead of selecting an animation at random.
It's Cycling properly But The Anims are looping, if I change them over to Triggers do I have to change the code and If so How? also He Wont Exit out of Punching When I hit the F $$anonymous$$ey Heres the code I have all together
private void Punching()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
IsPunching = !IsPunching;
if (IsPunching)
{
_animator.SetTrigger("IsPunching");
}
else
{
_animator.SetTrigger("DontPunch");
}
}
string[] animations = { "PunchingLeftJab", "PunchingStraightRight",
"PunchingStraightLeftHook","ThrowingAnElbow","PunchingRightHook" };
string chosenAnim = "PunchingLeftJab";
if (Input.GetButtonDown("Fire1"))
{
IsPunching = !IsPunching;
if (IsPunching)
{
int selection = Random.Range(0, animations.Length);
chosenAnim = animations[selection];
_animator.SetBool(chosenAnim, true);
}
else
{
_animator.SetBool(chosenAnim, false);
}
}
The code is the same if they're triggers and not bools, except that you don't set them to true or false. Without seeing the animation controller it's hard to know exactly how you have it configured. Is there an idle animation that the punches are transitioned from? If so, is it a different idle than the standing idle animation? Either way I'd have the punching state handled with a bool, not two separate triggers. Triggers are set and then reset immediately so if you have one of the punch animations playing when it's set and that animation doesn't have a transition prepared for that trigger nothing will happen. If you have the base animation from which the punches are called handled with a "punching" bool, you can set that bool to false (which will still be the case when that state is returned to from the individual punch animations) and that will allow the state to transition back to the not punching animation.
@Chimer0s I have it all setup Properly Now, everything is set to bool Transitioning into a selected punch is True and then Transitioning out is set to false. The only anim that doesn't loop is the PunchingLeftJab it plays fine, the rest will cycle ($$anonymous$$aybe) if I spam Press the mouse button then once and a blue moon it will cycle to a random anim but its unlikley and they all loop Besides the one. Any Ideas?
Edit: @Chimer0s Never $$anonymous$$ind I Figured It Out. THAN$$anonymous$$S! YOU DA $$anonymous$$AN!