Unity:歩いて止まるアニメーション例

Unity

PlayerController

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

public class PlayerController: MonoBehaviour
{
    public float Speed;     // 歩く速度
    Animator animator;      // Animatorコンポーネント取得用

    // Start is called before the first frame update
    void Start()
    {
        // Animatorコンポーネント取得
        animator = GetComponent<Animator>();
        // 歩くアニメーションを設定
        animator.SetBool("walk", true);
    }

    // Update is called once per frame
    void Update()
    {
        // 前に進ませる
        transform.position += Vector3.forward * Speed * Time.deltaTime;

        // 停止位置
        if(transform.position.z > 8f)
        {
            Speed = 0;
            // 歩くアニメーションを停止(アイドル状態にする)
            animator.SetBool("walk", false);
        }
    }
}

Animator Controller作成イメージ(Idle <->Walk間にパラメタ walk をBool型で作成)

補足

Conditions

Idle -> Walk walk が true
Walk -> Idle walk が false

コメント