行业知识
Unity 让角色动起来(动画控制器)
2026-11-12 11:07:43
来源:互联网转载
下载���材:
导入后,找到预制体和动画。
新建动画控制器,拖动到预制体的新版动画组件上。
建立动画关系
创建脚本,挂载到预制体上。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class c1 : MonoBehaviour{ // 获取动画组件 private Animator animator; void Start() { // 加载动画组件 animator = GetComponent(); } // Update is called once per frame void Update() { // 水平轴 float horizontal = Input.GetAxis("Horizontal"); // 垂直轴 float vertical = Input.GetAxis("Vertical"); // 向量 Vector3 dir = new Vector3(horizontal, 0, vertical); if (dir != Vector3.zero) { // 面向向量 transform.rotation = Quaternion.LookRotation(dir); // 播放跑步画面 animator.SetBool("run1", true); // 朝向前方移动 transform.Translate(Vector3.forward * 2 * Time.deltaTime); } else { // 站立动画 animator.SetBool("run1", false); } }} 可以选择调整两个动画之间的过渡时间: