0
\$\begingroup\$

I'm developing a 2D Wrestling game in Unity 5 build 5.1.2f1. It's just the beginning so the character only has a number of animation states and a rigidbody2d. This is the state machine enter image description here

and these are the Animator and Rigidbody2D components

enter image description here

Note that I'm using Root motion to animate "High Knee" state since I've animated the character jumping in the air(high knee) by animating character's transform.

This is the simplest form of code to move the character

void Start ()
{
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
}  

void Update ()
{
    print (rb.velocity);
    if(Input.GetAxis("Horizontal") > 0)
    {
        rb.AddForce(new Vector2(0,50));
        anim.SetBool("isWalking",true);
    }

    else if(Input.GetAxis("Horizontal") < 0)
    {
        rb.AddForce(new Vector2(0,-50));
        anim.SetBool("isWalking",true);
    }

    else if(Input.GetAxis("Vertical") > 0)
    {
        rb.AddForce(new Vector2(50,0));
        anim.SetBool("isWalking",true);
    }

    else if(Input.GetAxis("Vertical") < 0)
    {
        rb.AddForce(new Vector2(-50,0));
        anim.SetBool("isWalking",true);
    }

}

The Character starts moving in x direction with a constant speed as soon as i press the horizontal keys. But for the y axis, when i press up or down key, character starts at 0 and speeds up really really fast( like 50 milisecs) and then the speed keeps increasing and increasing until i release the key. It still keeps moving but the speed doesn't increase anymore. I'm not sure but i think the former behavior is weird and latter is fine (or vice versa). When i remove all the "anim.SetBool" lines in the above code, speed becomes constant for the y axis too, that is, it starts behaving exactly like the x axis. But when i disable the Animator component, x axis movement starts behaving like y axis, that is, speed starts at 0 and quickly increases and keeps increases as long as the key is held down. When i uncheck the "Apply Root Motion" check box, character doesn't move at all. I don't know what's happening. Any help would be appreciated.

\$\endgroup\$
  • \$\begingroup\$ Hey I think , the problem is due to some animations have root motions and some don't . Also make sure you click on "Generate Root Motion" for your High Knee animation. \$\endgroup\$ – Hash Buoy Sep 14 '15 at 5:22
  • \$\begingroup\$ Thanks for replying. Yes you are right only the high knee animation has Root Motion generated and rest don't. Should i generate root motion for all animations? \$\endgroup\$ – Jaspreet Singh Sep 14 '15 at 15:40
  • \$\begingroup\$ Yes try that it might help. I had similar issue don't know what solved it, but making all the animation have root motion was one of the tweaks i applied. \$\endgroup\$ – Hash Buoy Sep 14 '15 at 16:29
  • \$\begingroup\$ For some reason all other animations don't have the option to generate root motion curves. What could be the reason? \$\endgroup\$ – Jaspreet Singh Sep 14 '15 at 17:16
  • \$\begingroup\$ A wild guess , try adding any of the transform property in animation clip. Also make sure that they are not legacy animations. \$\endgroup\$ – Hash Buoy Sep 14 '15 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.