PlayMaker Feedback > Action Requests

Play Random Animation

(1/3) > >>

@_rgkd:
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:
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:

--- Quote from: Robert-Glen on June 01, 2011, 02:34:31 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.
--- End quote ---

That's actually a very sensible request for a new action, I like the idea.

tobbeo:
I clearly didn't read your post properly. Sorry.

Armada Animations:
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: ---// (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);
}
}

}
}
--- End code ---

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

Armada Animations - David.

Navigation

[0] Message Index

[#] Next page

Go to full version