photo

[SOLVED] Changing to different animations

Recommended Posts

Hello,

im trying to switch between animations when pressing a key but it is not working:

image.png.0deef78e9ecbb240393b1309985c9a4b.png

the only key that works is D because the 'idle' animation is set on layer 0, but the others are not working

also the animation does not restart, it looks like it picks it up from a specific point as if the animation is still playing in the background.

i saw the examples of 'AnimationAdditive' and 'SimpleAnimationPlayer' but i want to change from 1 animation to the other, not combine them.

can somebody help me with this?

Share this post


Link to post

Kireita

As far as I can see you got this working (from a discord server). Could you please post a final solution for this task?

Thanks!

Share this post


Link to post

@silent

hey, i did manage to it working the way i wanted. here is a snippet of the code:
 

    void Update()
    {
        forward = Input.IsKeyPressed(simplifiedPlayerController.forwardKey);
        backward = Input.IsKeyPressed(simplifiedPlayerController.backwardKey);
        left = Input.IsKeyPressed(simplifiedPlayerController.moveLeftKey);
        right = Input.IsKeyPressed(simplifiedPlayerController.moveRightKey);
        jump = Input.IsKeyPressed(simplifiedPlayerController.jumpKey);

        if (forward || left || right || backward)
        {
            WalkForward();
        }

        if (jump)
        {
            Jump();
        }

        if (actionKey)
        {
            // compute animation time
            animationTime += 60 * Game.IFps;

            // set animation frame
            mesh.SetFrame(animation, animationTime);

            // get actual animation time after set animationTimeJump
            float time = mesh.SetFrame(animation, animationTime);

            // copy layer data to root layer
            mesh.CopyLayer(-1, animation);

            // check if animation ended
            if (time < animationTime)
            {
                actionKey = false;
                instantAnimation = true;
            }
            else if (instantAnimation)
            {
                actionKey = false;
            }

            // reset idle animation
            animationTimeIdle = 0;
        }
        else
        {
            idle();
        }
    }

    public void WalkForward()
    {
        if (instantAnimation)
        {
            animation = (int)Layer.Walk;
            actionKey = true;
            instantAnimation = true;
        }
    }

    public void Jump()
    {
        if (instantAnimation)
        {
            actionKey = true;
            animationTime = 0;
            animation = (int)Layer.Jump;
            instantAnimation = false;
        }
    }

    public void idle()
    {
        animation = (int)Layer.Idle;
        animationTime = 0;
        animationTimeIdle += 60 * Game.IFps;

        // set animation frame
        mesh.SetFrame(animation, animationTimeIdle);
    }

i wanted the possibility of having a forced animation and instant animations, for example jump cannot be canceled so you need to wait for the animation to end and walk can be overwritten by any other action, its not complete but its a start :D.

any suggestions would be very much appreciated

and this case can be closed.

thank you for the help guys!

Share this post


Link to post

In attachment you can find quick and dirty prototype of the forced animation playing:

image.png

component_animation.zip

  • Like 1

Share this post


Link to post