Dragon Flea
Dragon Flea

Reputation: 319

Why when creating new animator controller for the character the character is not walking right?

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.

Animator Controller

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.

Animator Controller.

And 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;
        }
    }
}

Upvotes: 0

Views: 224

Answers (1)

ZayedUpal
ZayedUpal

Reputation: 1601

First of all, you are mixing between 2 methods. You are playing the animations from the code and also assigning trasitions in animator. They will conflict. You either do the whole animation control from code or follow these steps: 1. Make a finite state machine in the animator window using transitions. 2. Add parameters to those transitions(bool, int etc) 3. Change the value of the parameters from code like anim.SetBool("walk", true) to control animations.

Another thing, don't forget to set the idle or walk animation's wrap mode to loop. Otherwise, they'll play once and then stop.

Upvotes: 1

Related Questions