DragonBonesCSharp
DragonBonesCSharp copied to clipboard
Animation is not playing problem
Hi there, I have created a simple animation using Dragonbone pro, now when I want to use that animation in unity it stuck on last frame in another word no animation is played but stack on one frame. Here is the code I use:
`using System.Collections; using System.Collections.Generic; using UnityEngine; using DragonBones;
public class Player : MonoBehaviour {
private UnityArmatureComponent player;
// Use this for initialization
void Start () {
player = this.gameObject.GetComponent<UnityArmatureComponent>();
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.LeftArrow)) {
player.animation.Play ("walk", -1);
} else if(Input.GetKey(KeyCode.LeftArrow)){
player.animation.Play ("walk", -1);
} else {
player.animation.Play ("stand", -1);
}
}
} `
Any help is much appreciated.
Hi, please use GetKeyDown instead of GetKey.
Hi, please use GetKeyDown instead of GetKey.
hi @akdcl I tried to use it with GetKeyDown but still no success; I found that all animation is works fine on if you use it on Start Method but it stuck if you use it in side Update Method. anytip?
The issue here is that every update you are starting an animation. This means either the walk or stand animation is starting every single frame, so it never goes past the first frame.
This might work a bit better:
void Update()
{
bool moveKeyPressed = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow);
bool moveKeyUp = Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow);
bool moving = Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow);
// if any move key was pressed down this frame, start walk animation
if (moveKeyPressed){
player.animation.Play("Walk", -1);
}
// else if not moving at all and a movement key was released
else if (!moving && moveKeyUp){
player.animation.Play("stand", -1);
}
}
The gist of it is you only play the stand animation the frame that the player stops moving completely.
The issue here is that every update you are starting an animation. This means either the walk or stand animation is starting every single frame, so it never goes past the first frame.
This might work a bit better:
void Update() { bool moveKeyPressed = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow); bool moveKeyUp = Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow); bool moving = Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow); // if any move key was pressed down this frame, start walk animation if (moveKeyPressed){ player.animation.Play("Walk", -1); } // else if not moving at all and a movement key was released else if (!moving && moveKeyUp){ player.animation.Play("stand", -1); } } The gist of it is you only play the stand animation the frame that the player stops moving completely.
Thanks! @chriswaldron15 it really solved my problem; however I still did not understand the concept of how animation.Play(name, time) works. like if it can't and why its not working the regular way like unity animation does?
No problem!
So basically before, an animation was being started every frame, and usually the same animation. DragonBones won't check whether that animation is already playing and ignore the new Play call, so it will start the animation from its first frame every Update call. So the animation never gets past the first frame.
Perhaps Unity's animation checks whether that animation is already playing and so doesn't start it from the beginning again, but it's a bit more complicated because it uses states.
If you want to check whether an animation is already playing, you can call:
player.animation.GetState("Walk").isPlaying;
This might be cleaner than the code I was providing! And then only call Play("Walk") if that animation is not playing.
No problem!
So basically before, an animation was being started every frame, and usually the same animation. DragonBones won't check whether that animation is already playing and ignore the new Play call, so it will start the animation from its first frame every Update call. So the animation never gets past the first frame.
Perhaps Unity's animation checks whether that animation is already playing and so doesn't start it from the beginning again, but it's a bit more complicated because it uses states.
If you want to check whether an animation is already playing, you can call:
player.animation.GetState("Walk").isPlaying;
This might be cleaner than the code I was providing! And then only call Play("Walk") if that animation is not playing.
THANKS! that is what I really wanted to know.