- Home /
Mecanim Animation Using Sprite Sheets
I am using sprite sheets to animate my character that I have on a GameObject with a SpriteRender and an Animator.
I have 6 sprites for each running direction and use one sprite from each direction as my idle pose.
I have created 8 animations: 4 for each run direction and 4 for each idle direction.
My problem is now within the Mecanim Animation Controller. I have a boolean parameter "moving" and an integer "direction" that store the direction (0=up, 1=right, 2=down, 3=left).
I want any of the 8 animations to immediately interrupt another one so that for example changing from running Right to Up is instantaneous.
Creating transitions to each animation from Any State (with specific moving and direction parameters) doesn't work since Any State can trigger a transition from a state to itself (see here). So basically while the character is running up, the Running Up animation keeps on getting continuously triggered.
Is there an easy way to work around this at the moment, preferably with no scripting since I feel everything to do with animation should stay within Mecanim ? Why hasn't an option been put in to exclude the current state from Any State, it seems like a trivial feature?
For now I have put a Trigger parameter "changed" and in my player script i set that trigger when anything changes and put a trigger condition in every transition but that's really not ideal.
Am I missing something obvious?
Thanks!
Answer by ImpishMario · Dec 27, 2013 at 01:37 PM
Hey! guess you can try at least 2 solutions:
you can try to avoid "Any State" and create generic Idle state and then make transitions to Idle Left/Right/Up/Down (maybe as a sub-states?) and then to link transitions to proper animation states - this should eliminate continuous triggering "feature" ;) I, however, have had some issues with this solution (apart from spaghetti structure) e.g. I have no idea why but one of my animations was looping (2 playbacks in total) while it wasn't supposed to loop at all. Therefore I decided to go with second solution;
Use Any State and try to write a simple coroutine that manages your SetTrigger/SetBool whatever method you're using. This can go like this (I use it for my SetTrigger):
using UnityEngine; using System.Collections;
public class InterfaceAnimation : MonoBehaviour {
public Animator animationObject; static private InterfaceAnimation Instance; static private bool canAnimate; void Start () { Instance = this; canAnimate = true; } public static IEnumerator Trigger (string trigger) { if (canAnimate) { Instance.animationObject.SetTrigger (trigger); canAnimate = false; } yield return new WaitForSeconds (3f); canAnimate = true; } }
Then you just call your coroutine and it makes sure that your trigger is not set continuously!
Hope this helps!
Answer by SuarescorX · Feb 25, 2014 at 10:26 PM
Hi, it happened the same to me, all i did was save the integer into a variable before setting the animation, then, I checked if the value of the new animation was different of current. It helped me a lot in terms of optimization, since i was using it with 50 characters simultaneously
int currentAnim
void SetAnimation(int newAnim)
{
if(newAnim != currentAnim)
{
this.getComponent<Animator>().SetInteger(newAnim);
currentAnim = newAnim;
}
}
Your answer
Follow this Question
Related Questions
Simultaneous Animation for an Object 0 Answers
I cannot make AnimationClip changing some Sprites without AnimationController. 0 Answers
Object not moving after changing animator parameter 1 Answer
Sprite Rect Moves Between Frames 0 Answers
Keyframe animation WITHOUT curve (Sprite Animation) 1 Answer