S
S
src3032021-08-26 19:30:32
Unity
src303, 2021-08-26 19:30:32

How to make a character walk and run in Unity?

It is necessary to play the walking animation with this position of the joystick (I have the animation itself). Even in this position, my character slows down, but the walking animation is not enough to play (I also have animation for this):
6127bea34297c672473373.png

6127c02cb45a0642991494.png
And in this position, running works and the animation is played.

Animator:
6127c0a11ff1b120672251.png

Code PlayerController:

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

[RequireComponent(typeof(Rigidbody), typeof (BoxCollider))]
public class PlayerController : MonoBehaviour
{
    [SerializeField] private Rigidbody _rigidbody;
    [SerializeField] private FixedJoystick _joystick;
    [SerializeField] private Animator _animator;

    [SerializeField] private float _moveSpeed;

    private void FixedUpdate()
    {
        _rigidbody.velocity = new Vector3(_joystick.Horizontal * _moveSpeed, _rigidbody.velocity.y, _joystick.Vertical * _moveSpeed);

        if (_joystick.Horizontal != 0 || _joystick.Vertical != 0)
        {
            transform.rotation = Quaternion.LookRotation(_rigidbody.velocity);
            _animator.SetBool("Move", true);
        }
        else
            _animator.SetBool("Move", false);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Sokolov, 2021-08-26
@NikS42

Not this way. We need 1d BlendTree with float parameter Speed. At zero the animation is idle, at 0.5 you can walk for variability, and at 1 you can run. The Speed ​​parameter corresponds to the amount of tilt of the joystick, respectively. BlendTree interpolates different animations and can play half run, half walk, or even tiny steps at 10% of the joystick's max. In the latter case, it is important that the idle animation does not contain unnecessary movements, otherwise they will also be mixed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question