今天实现的内容:
动画机设计理念
要我说,动画机真的是老生常谈的东西了。做了好几次了,目前也就是将角色的地面动画做进去。
设计理念就是使用混合树ground将地面移动动画统一管理起来。
动画机的运用及模型旋转
要运用动画机,需要将动画机和输入模块串接起来。所以我们需要新的脚本,没错,PlayerController。同样的,顺手解决角色的旋转功能,旋转的思路为直接修改模型的forward。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// 玩家的人物模型 用来获取模型身上的动画机以及其它
public GameObject model;
// 输入模块
public PlayerInput pi;
// 动画机
private Animator anim;
// Awake适合用来做GetComponent
void Awake()
{
anim = model.GetComponent<Animator>();
pi = GetComponent<PlayerInput>();
}
// Update is called once per frame
void Update()
{
// 将输入转换为速度 赋值给动画机相关参数
anim.SetFloat("forward", pi.dirMag);
// 只在有速度时能够旋转 防止原地旋转
if(pi.dirMag > 0.1f)
model.transform.forward = pi.dirVec;
}
}
在我们的方案中,旋转的只是模型,PlayerController所在的父物体节点不会旋转
我们可以将旋转和输入的模长计算放到PlayerInput中进行,能让我们的代码看起来更漂亮。
// 玩家的输入模长量 用于当成向前量大小作为动画控制
public float dirMag;
// 玩家的方向 用于旋转模型
public Vector3 dirVec;
// Update is called once per frame
void Update()
{
// 计算输入模长
dirMag &