unity动画实现人物的功能


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

public class PlayerController : MonoBehaviour
{
    //玩家移动的速度
    public float moveSpeed = 3;
    //敌人移动的速度
    public float enemyspeed = 10;
    //玩家暂停的速度
    public float stopSpeed = 20;
    //移动的动画
    public Animator anim;
    //走路的声音
    public AudioSource _audio;
    private void Awake()
    {
        //初始化添加动画和音频的组件
        anim = this.GetComponent<Animator>();
        _audio = this.GetComponent<AudioSource>();
    }
    private void FixedUpdate()
    {
        //判断动画是否播放
        if (anim.GetCurrentAnimatorStateInfo(0).IsName("Tree"))
        {
            //判断音频是否播放
            if (!_audio.isPlaying)
            {
                _audio.Play();
            }
        }
    }
    private void Update()
    {
        //用WASD来控制玩家移动
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //判断水平轴和垂直轴是否为空
        if (h != 0 || v != 0)
        {
            //用插值来储存最大的速度
            float tempSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 5.6f, moveSpeed * Time.deltaTime);
            //改变之前的值
            anim.SetFloat("Speed", tempSpeed);
            //用vector3来储存水平轴和垂直轴
            Vector3 vet = new Vector3(h, 0, v);
            //四元数时用来旋转用的
            Quaternion newVet = Quaternion.LookRotation(vet);
            transform.rotation = Quaternion.Lerp(transform.rotation, newVet, enemyspeed * Time.deltaTime);
        }
        else
        {
            //用插值来储存最小的速度,不按键的时候让人物静止不动
            float tempSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 0, stopSpeed * Time.deltaTime);
            anim.SetFloat("Speed", tempSpeed);
        }
        if (Input.GetKey(KeyCode.LeftShift))//按SHIFT静步走
        {
            //按SHIFT时动画播放
            anim.SetBool("Sneak", true);
        }
        else
        {
            //不按SHIFT时动画不播放
            anim.SetBool("Sneak", false);
        }
    }



}

```后续还有许多,  
不足之处,还请多多包涵。

猜你喜欢

转载自blog.csdn.net/weixin_44370124/article/details/92560856