1.将动作导入Blend Tree中,利用threshoder修改动作时间
2.玩家键入通过程式化控制角色动作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
[Header("==== Key settings ====")]
public string keyUp = "w";
public string keyDown = "s";
public string keyLeft = "a";
public string keyRight = "d";
public string keyA;
public string keyB;
public string keyC;
public string keyD;
[Header("==== Output signal ====")]
public float Dup;
public float Dright;
public float Dmag;
public Vector3 Dvec;
//1.pressing signal 按压式
public bool run;
//2.trigger once signal 一次性触发
//3.double trigger
[Header("==== Others ====")]
public bool inputEnabled = true;
private float targeDup;
private float targeDright;
private float velocityDup;
private float veolcityDright;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//keyup keyright 转 signal
targeDup = (Input.GetKey(keyUp) ? 1.0f : 0) - (Input.GetKey(keyDown) ? 1.0f : 0);
targeDright = (Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0);
if(inputEnabled == false)//软开关 清零targeDup targeDright
{
targeDup = 0;
targeDright = 0;
}
//平滑阻尼 不会很僵硬变到某一个值
Dup = Mathf.SmoothDamp(Dup, targeDup, ref velocityDup, 0.1f);
Dright = Mathf.SmoothDamp(Dright, targeDright, ref veolcityDright, 0.1f);
Dmag = Mathf.Sqrt(Dup * Dup) + (Dright * Dright);
//坐标系计算+动画补间旋转
Dvec = Dright * transform.right + Dup * transform.forward;
run = Input.GetKey(keyA);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActorController : MonoBehaviour
{
public GameObject model;
public PlayerInput pi;
public float WalkSpeed = 2.0f;
public float runMultiplier = 2.0f;//跑步速度 可视化控制
[SerializeField]//把Animator暂时显示到unity
private Animator anim;
private Rigidbody rigid;//Rigidbody 不能在updata中调用 要在Fixedupdata中调用
private Vector3 movingVec;//用于储存玩家意图
// Use this for initialization
void Awake()
{
//获取组件
pi = GetComponent<PlayerInput>();
anim = model.GetComponent<Animator>();//吃一个模型
rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//坐标系计算+动画补间旋转
anim.SetFloat("forward", pi.Dmag*((pi.run)?2.0f:1.0f));//把Dup的值喂给Animator里面的forwad
if (pi.Dmag > 0.1f)//增加判断 如果按键时间大于0.1秒那么就不转回到前面
{
model.transform.forward = pi.Dvec;
}
movingVec = pi.Dmag * model.transform.forward*WalkSpeed*((pi.run)?runMultiplier:1.0f);//玩家操作存量大小*当下模型正面=玩家意图 把玩家意图存在这个变量里面
}
private void FixedUpdate() //undata是每秒60帧,达到和显示屏一样的刷新速度 而fixedupdata(物理引擎) 每秒50帧
{
//rigid.position += movingVec * Time.fixedDeltaTime;//增加一段距离()每秒1个单位 速度乘时间来改位移
rigid.velocity = new Vector3(movingVec.x, rigid.velocity.y, movingVec.z);//直接指派速度
}
}