Munggo Lloyd
Munggo Lloyd

Reputation: 25

UNITY Animation setFloat not working properly

I'm having an on screen buttons that controls the movement of the character, Walk Right, Walk Left and Jump. My Walk Left and Jump button events are working properly with the animation but the right button are just moving the character and not playing the walk Animation, I use setFloat to detect the movement of the character please help me Identify why the walk right animation are not playing.

My script for the movement controls:

void Movement()
{

    if (RightPressed)
    {
        target.Translate(Vector2.right * 3f * Time.deltaTime);
        target.eulerAngles = new Vector2(0, 0);
    }

    if (LeftPressed)
    {
        target.Translate(-Vector2.right * 3f * Time.deltaTime);
        target.eulerAngles = new Vector2(0, 0);
    }

    if (SpacePressed && isJumping == true)
    {

        if (jumpTimeCounter > 0)
        {
            rb.velocity = Vector2.up * jumpForce;
            rb.freezeRotation = true;
            jumpTimeCounter -= Time.deltaTime;
            anim.SetBool("IsJumping", true);
        }
        else
        {
            isJumping = false;
        }
    }
}

My Update function:

void Update () {

    Movement();
    {

        if (RightPressed)
            anim.SetFloat("speed", 0.1f);
        else
            anim.SetFloat("speed", 0.0f);


        if (LeftPressed)
            anim.SetFloat("speed", -0.1f);
        else
            anim.SetFloat("speed", 0.0f);

        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

    } 

}

My function for the On screen Buttons

public void OnScreenButtonChange(string dir)
{
    switch (dir)
    {
        case "A":
            LeftPressed = !LeftPressed;
            break;
        case "D":
            RightPressed = !RightPressed;
            break;
        case "Space":
            SpacePressed = !SpacePressed;
            break;
    }
}

Upvotes: 0

Views: 3531

Answers (2)

TheStonedRaider
TheStonedRaider

Reputation: 1

The 2 values after anim.SetFloat("frequencyMultiplier", beatFrequency); also need to be set e.g anim.SetFloat("frequencyMultiplier", beatFrequency,0.1f,0.1f);

Upvotes: 0

Rakiah
Rakiah

Reputation: 240

Because your RightPressed code block get overrided by LeftPressed codeblock,

basically, if you would flip around like this

    if (LeftPressed)
        anim.SetFloat("speed", -0.1f);
    else
        anim.SetFloat("speed", 0.0f);

    if (RightPressed)
        anim.SetFloat("speed", 0.1f);
    else
        anim.SetFloat("speed", 0.0f);

you would notice that your problem is reversed, you're using in both case a boolean that must be either false or true, and when LeftPressed is false, you're still updating the speed of the animation to 0.0f, which override what was set in RightPressed, what you want to do is to change the way you organized your code

    if (LeftPressed)
        anim.SetFloat("speed", -0.1f);

    if (RightPressed)
        anim.SetFloat("speed", 0.1f);

    if ((!LeftPressed && !RightPressed) || (LeftPressed && RightPressed))
        anim.SetFloat("speed", 0.0f);

this will work better but is not a very good way to do so, you should synchronize the animation with actual velocity of your rigidbody/transform, to make sure you don't have desyncs between the movement of the object and the animation

Upvotes: 2

Related Questions