playMaker

Author Topic: Play Random Animation  (Read 10444 times)

@_rgkd

  • Playmaker Newbie
  • *
  • Posts: 7
    • @_rgkd on Twitter
Play Random Animation
« on: June 01, 2011, 02:34:31 AM »
Hopefully it's possible to implement a "Play Random Animation" Action please. Currently I'm attempting to break up my characters Idle animation and maybe I'm doing it wrong but I transition away from the state after a Wait period with FINISH, perform a Random Int Action and depending on the outcome branch off to different other Play Animation states then loop back.

Main problem is I have to set up even more transitions and Actions since I now have multiple States rotating, its messy and hard to expand. Ideally I think it could work like the "Select Random Colour" Action that would allow you to choose/expand the amount of animations, assign them and give each an individual weight/chance to play. Extra properties might be an option to Delay at the end before repeating and allowing the user to override the animation default wrap mode with something else.

I'm no code genius, hence the reason I bought playMaker.

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: Play Random Animation
« Reply #1 on: June 01, 2011, 03:32:43 AM »
Well, the way I figured out how to do this (and I am no coder either), is by generating a random integer value using random int under logic. Then I use a "compare int" action to send it onwards to other states that play an animation. That gives you a way of randomly playing an animation. It gives you a way of randomly doing a lot of things. This isn't the only solution in playmaker but one that I use a lot.

Just because you don't have to code doesn't mean there's no learning curve at all! Playmaker just makes it a lot easier, you still have to think and problem solve!

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Play Random Animation
« Reply #2 on: June 04, 2011, 07:35:06 AM »
Ideally I think it could work like the "Select Random Colour" Action that would allow you to choose/expand the amount of animations, assign them and give each an individual weight/chance to play. Extra properties might be an option to Delay at the end before repeating and allowing the user to override the animation default wrap mode with something else.

That's actually a very sensible request for a new action, I like the idea.
--
Breno "MaDDoX" Azevedo
@brenoazevedo

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: Play Random Animation
« Reply #3 on: June 04, 2011, 08:32:17 PM »
I clearly didn't read your post properly. Sorry.

Armada Animations

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Play Random Animation
« Reply #4 on: March 31, 2012, 05:43:30 AM »
I notice this post, and I have not seen an answer. I am very new to playmaker but I believe the answer to a custom action which plays a random animation would be something like this..

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
// Random factor created by Armada Animations

using System;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Animation)]
[Tooltip("Plays an Random Animation on a Game Object.")]
public class PlayRandomAnimation : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Animation))]
[Tooltip("Game Object to play the animation on.")]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.Animation)]
[Tooltip("The name of the animation to play.")]
[CompoundArray("Anim Names", "Anim Name", "Play Mode")]
public FsmString[] animName;

[Tooltip("How to treat previously playing animations.")]
public PlayMode[] playMode;

[Tooltip("The behavior of the animation when it wraps.")]
public WrapMode wrapMode;

[HasFloatSlider(0f, 5f)]
[Tooltip("Time taken to blend to this animation.")]
public FsmFloat blendTime;

[Tooltip("Event to send when the animation is finished playing. NOTE: Not sent with Loop or PingPong wrap modes!")]
public FsmEvent finishEvent;

[Tooltip("Event to send when the animation loops. If you want to send this event to another FSM use Set Event Target. NOTE: This event is only sent with Loop and PingPong wrap modes.")]
public FsmEvent loopEvent;

[Tooltip("Stop playing the animation when this state is exited.")]
public bool stopOnExit;

private AnimationState anim;
private float prevAnimtTime;
private int randomIndex;

public override void Reset()
{
gameObject = null;
animName = new FsmString[1];
//animName = null;
playMode = new PlayMode[] {PlayMode.StopAll};
wrapMode = WrapMode.Once;
blendTime = 0.3f;
finishEvent = null;
loopEvent = null;
stopOnExit = false;
}

public override void OnEnter()
{
DoPlayAnimation();
}

void DoPlayAnimation()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (animName.Length == 0) return;

//int randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
randomIndex = UnityEngine.Random.Range(0, animName.Length-1);

if (go == null || string.IsNullOrEmpty(animName[randomIndex].Value))
{
Finish();
return;
}

if (string.IsNullOrEmpty(animName[randomIndex].Value))
{
LogWarning("Missing animName!");
Finish();
return;
}

if (go.animation == null)
{
LogWarning("Missing animation component!");
Finish();
return;
}

anim = go.animation[animName[randomIndex].Value];

if (anim == null)
{
LogWarning("Missing animation: " + animName[randomIndex].Value);
Finish();
return;
}

var time = blendTime.Value;

if (time < 0.001f)
{
go.animation.Play(animName[randomIndex].Value, playMode[randomIndex]);
}
else
{
go.animation.CrossFade(animName[randomIndex].Value, time, playMode[randomIndex]);
}

anim.wrapMode = wrapMode;
prevAnimtTime = anim.time;
}

public override void OnUpdate()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null || anim == null)
{
return;
}

if (!anim.enabled || (anim.wrapMode == WrapMode.ClampForever && anim.time > anim.length))
{
Fsm.Event(finishEvent);
Finish();
}

if (anim.wrapMode != WrapMode.ClampForever && anim.time > anim.length && prevAnimtTime < anim.length)
{
Fsm.Event(loopEvent);
}
}

public override void OnExit()
{
if (stopOnExit)
{
StopAnimation();
}
}

void StopAnimation()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go != null && go.animation != null)
{
go.animation.Stop(animName[randomIndex].Value);
}
}

}
}

I hope it helps anyone who is looking for a code like this.

Armada Animations - David.

lalamax3d

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Play Random Animation
« Reply #5 on: August 03, 2013, 03:03:22 PM »
any ideas, how to give list of animations to this action??
i have total 8 animations on animation component on object(character) idles list is 2,3,4. but i don't know to put them to play randomly
huge thanks in advance

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Play Random Animation
« Reply #6 on: August 06, 2013, 04:21:36 AM »
hi,

 simply set "Animations" to 8 and you will then get 8 slots to define them all.

bye,

 Jean

lalamax3d

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Play Random Animation
« Reply #7 on: August 09, 2013, 09:02:12 AM »
wow, thanks, its awesome..

lalamax3d

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Play Random Animation
« Reply #8 on: August 10, 2013, 04:16:29 AM »
it played once and stop, is it possible, that it kept looping randomly between those.. thanks in advance

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Play Random Animation
« Reply #9 on: August 12, 2013, 05:05:51 AM »
hi,

 you should then look for when audio stop playing and trigger a random pick again.

bye,

Jean

mikejkelley

  • Full Member
  • ***
  • Posts: 136
Re: Play Random Animation
« Reply #10 on: October 15, 2013, 02:05:41 PM »
hi, I've been looking for a way to loop the random pick too, I don't understand the audio solution. It would be super useful for some robust idle animations.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Play Random Animation
« Reply #11 on: October 16, 2013, 01:43:38 AM »
Hi,

You may want to look at an Audio Framework, they all pretty solve that kind of stuff, implement libraries of sounds and definitly implements feedback on audio playback and stop.

bye,

 Jean