0
\$\begingroup\$

I created a new controller called it SoldierController and dragged it to the character Controller under Animator component in the Inspector.

Also unchecked Apply Root Motion

Then attached a new script to the third person character called the script

Soldier.Not working good

Then i set the animator controller i added to it two new States: Walk and Idle. HumanoidIdle and HumanoidWalk.

Then i did that the default start state will be Idle. Set StateMachine Default State.

Then i did a Transition from Walk to Idle. This way when i press on W it's start walking a bit then it keep moving the character but without the walking animation.

If i delete this transition and make transition from the Idle to Walk then when i press on W it will walk but then if i leave W key and it's idling then after 2-3 seconds the character will walk on place i mean it will animate walking without moving and i'm not pressing anything it's starting automatic when idling.

The character had another animator controller but i created a new one and using only the new one.

Not working good.

The script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Soldier : MonoBehaviour
{
    private bool _isWalking = false;
    private Animator anim;

    // Use this for initialization
    void Start ()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update ()
    {
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;

        transform.Rotate(0, x, 0);

        if (Input.GetKey("w"))
        {
            if (!_isWalking)
            {
                _isWalking = true;
                anim.Play("Walk");
            }
            var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
            transform.Translate(0, 0, z); // Only move when "w" is pressed.
        }
        else
        {
            if (_isWalking)
            {
                anim.Play("Idle");
            }
            _isWalking = false;
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

remove the time.DeltaTime from where you want to pick up the horizontal input. In the animator create a horizontal movespeed parameter and set idle as the default anim. then when you transition set the param float 'horizontalspeed' to if greater than .1 it will transition to walk. then in your playerscript you can have the horizontal input update the parameter anim.SetFloat("HorizontalSpeed", Mathf.Abs(x));

\$\endgroup\$
  • \$\begingroup\$ I found that if i'm not making any transition between walk and idle it's working fine. so i wonder when should i make transition and use parameters and when not ? I just deleted the transition between idle and walk and it's working fine. But i guess that your solution will work too. I just wonder if should make transition or not ? \$\endgroup\$ – Dragon Flea Sep 2 '17 at 6:07
  • \$\begingroup\$ I typically like using the transitions and parameters cause you can wire up the playercontrollers values to auto update the params in animator. In your case though calling the anim. play () will ignore the state and transitions and play the anim itself instead but if that works for you then it should be fine. before the animator tab we used to code all the states and blends without a visual tab. \$\endgroup\$ – Justin Markwell Sep 2 '17 at 14:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.