Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
gomnosano

2D Glitchy Movement

Question

Alright, so I'm working on a 2D game, I made the running and idle animations but when I press A and D really fast and then hold one of them my character doesn't play the running animation and doesn't turn around.

 

public Animator anim;

    public Quaternion turn;

    void Start () {

        anim = this.GetComponent<Animator>();
        
    }

    void Update() {

        Forward();
        Back();
        //Jump();
        //Crouch();

    }

    void Forward()
    {

        if (Input.GetKeyDown(KeyCode.D))
        {

            turn.y = 0;
            this.transform.rotation = turn;
            anim.SetBool("isForwardDown", true);

        }

        if (Input.GetKeyUp(KeyCode.D))
        {

            anim.SetBool("isForwardDown", false);

        }

    }

    void Back()
    {

        if (Input.GetKeyDown(KeyCode.A))
        {

            turn.y = 180;
            this.transform.rotation = turn;
            anim.SetBool("isForwardDown", true);

        }

        if (Input.GetKeyUp(KeyCode.A))
        {

            anim.SetBool("isForwardDown", false);

        }

    }

Edited by gomnosano

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

How about we simplify it and just say:

 

public Animator anim;

public Quaternion turn;

 

private bool wasDown = false;

 

void Update()

{

float horizontal = Input.GetAxisRaw("Horizontal");

if(horizontal != 0)

{

turn.y = 90 + (horizontal * 90);

transform.rotation = turn;

if(!wasDown) anim.SetBool(isForwardDown", true);

wasDown = true;

}

else

{

if(wasDown) anim.SetBool("isForwardDown", false);

wasDown = false;

}

}

 

 

ideally, you would actually want to animate based on velocity and grounded instead of input but the example above is based on the code you posted. The reason is that if you hit a wall and are no longer actually moving, your animator would still think it should animate. Using velocity fixes that issue

  • Like 1

Share this post


Link to post
Share on other sites

  • 0

And I know it's already answered and way overkill. But instead of rotating, you can also just say:

 

if(horizontal != 0) transform.localScale.x = horizontal;

 

and thats it. -1 in scale actually flips it for you. Just FYI for the future

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...