LoginSignup
14
12

More than 5 years have passed since last update.

【Unity】フットスライディング(足滑り)を解決する

Last updated at Posted at 2019-01-16

フットスライディング(足滑り)

foot2.gif
キャラクターの動く速度と、アニメーションが同期してない場合に、
フットスライディング(足滑り)が起こります。

foot1.gif
こんな感じに、リアルで「地に足がついている」ように見せる方法を紹介します。

※ キャラクターををお借りしました
Sci-Fi Soldier

解決方法

前提として、3DCGソフト側でモーションに合わせて実際に移動させておきます。
その移動量を使って、ゲーム中の移動とアニメーションを同期させます。

using UnityEngine;

// Enable animator ApplyRootMotion
// Add movement amount of Z in animation
public class CharacterController : MonoBehaviour
{
    [SerializeField]
    float _moveSpeed;

    Animator _animator;
    float _movementInWalk;

    private static readonly int _walkNameHash = Animator.StringToHash("Walk");

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

        _movementInWalk = ExamineMovement(_walkNameHash);

        Walk();
    }

    void OnAnimatorMove()
    {
        // Update movement in animation
        var deltaPos = _animator.deltaPosition;
        transform.localPosition += deltaPos;
    }

    // Measure the amount of movement to the Z axis during animation
    private float ExamineMovement(int nameHash)
    {
        _animator.Play(nameHash);
        _animator.Update(0f);

        var currentClip = _animator.GetCurrentAnimatorClipInfo(layerIndex: 0)[0].clip;
        _animator.Update(currentClip.length);

        var movement = Vector3.Project(_animator.deltaPosition, transform.forward).magnitude / currentClip.length;
        return movement;
    }

    private void Walk()
    {
        _animator.speed = _moveSpeed / _movementInWalk; // Adjust the animation speed according to the moving speed
        _animator.PlayInFixedTime(_walkNameHash, layer: 0, fixedTime: 0f);
    }
}

ExamineMovementメソッドでアニメーション中での移動量を計測しています。
(ここではZ軸の移動量のみを対象にしています)

Walkメソッドで、アニメーションの再生速度を調整しています。
ゲーム中での移動量と、アニメーション中での移動量が同期するように、再生速度を設定しています。

OnAnimatorMoveメソッドをオーバーライドして、アニメーション中の移動量をゲームにそのまま反映します。

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12