Jump to content

[SOLVED] Changing to different animations


photo

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?

Link to comment

@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!

Link to comment
  • silent changed the title to [SOLVED] Changing to different animations
  • 4 months later...

Hi @silent,

Your forced animation example is excellent, I was able to quickly set up a player controller. What I was unable to do is make the transition between animations smooth.

I've tried using the following method, instead of SetLayerEnabled to add a blend weight as the docs state but it doesn't have any effect:
 

playerMesh.SetLayer((int)AnimLayers.IDLE, false, 0.5f);
playerMesh.SetLayer((int)AnimLayers.RUN_FORWARD, false, 0.5f);

Any idea what we may be missing here?

Link to comment
×
×
  • Create New...