p333-
![](https://i-blog.csdnimg.cn/direct/d6172a17ee614cdba100266121bc8bcb.png)
![](https://i-blog.csdnimg.cn/direct/75375ba2d8e14c6eb8135244a49d032a.png)
![](https://i-blog.csdnimg.cn/direct/6ce5d08a1aa343389b6e1794835daa0a.png)
![](https://i-blog.csdnimg.cn/direct/dbd69c87edfb488da65494c3e189b861.png)
声音组件添加
![](https://i-blog.csdnimg.cn/direct/959b00ea27df4552ad2bbe39fc651937.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RobotAnimationController : MonoBehaviour
{
[Header("平滑过渡时间")]
[Range(0,3)]
public float dampTime = 3f;
[Header("移动速度")]
public float speed = 3f;
[Header("转身速度")]
public float turnSpeed = 10f;
//虚拟轴
private float hor, ver;
//动画组件
private Animator ani;
//声音组件
private AudioSource aud;
//虚拟按键
private bool sneak;
private void Awake()
{
ani = GetComponent<Animator>();
aud = GetComponent<AudioSource>();
}
private void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
//获取虚拟按键
sneak = Input.GetButton("Sneak");
//if (sneak) {
// ani.SetBool("Sneak", sneak);
//}
//设置动画参数
ani.SetBool("Sneak", sneak);
//只要按下一个方向键,就走
if (hor != 0 || ver != 0)
{
//设置Speed,角色动起来
ani.SetFloat("Speed", 5.6f, dampTime, Time.deltaTime);
//将向量转换成四元数
Quaternion targetQua = Quaternion.LookRotation(new Vector3(hor, 0, ver));
//lerp过去
transform.rotation = Quaternion.Lerp(transform.rotation, targetQua, Time.deltaTime * turnSpeed);
}
else {
//停下来
ani.SetFloat("Speed", 0f);
}
//判断当前是否是Locomotion状态
bool isLocomotion=ani.GetCurrentAnimatorStateInfo(0).IsName("Locomotion");
if (isLocomotion)
{
//判断脚步声音是不是已经开始播放了
if (!aud.isPlaying) {
//播放声音
aud.Play();
}
}
else {
//停止声音
aud.Stop();
}
}
}
![](https://i-blog.csdnimg.cn/direct/44434da693844f32bfb0b442862f288a.png)
边走路边喊叫 动画分层的应用
![](https://i-blog.csdnimg.cn/direct/fdd6c55007744fca8decb9415f756c94.png)
Mask
![](https://i-blog.csdnimg.cn/direct/226e700cdff94d418ec1b6cbe16a8476.png)
![](https://i-blog.csdnimg.cn/direct/add22c4c3e3242088b06deda84789587.png)
模型正对我们
![](https://i-blog.csdnimg.cn/direct/8895a9f8d02142c791f0d78b04c68bc9.png)
非人型遮罩------手动勾选
![](https://i-blog.csdnimg.cn/direct/b9403423a831445393ec5dda7c373c14.png)
![](https://i-blog.csdnimg.cn/direct/c94ce572abec4aa6bc2d891f5686807b.png)
![](https://i-blog.csdnimg.cn/direct/fbe38477fd024b06be71e8dc4bc90c30.png)
![](https://i-blog.csdnimg.cn/direct/6eb079376cc84acd942d2869ebab98d4.png)
override覆盖上一层的动画
![](https://i-blog.csdnimg.cn/direct/3378224b50ce4064bb096fe574446522.png)
![](https://i-blog.csdnimg.cn/direct/631bc7e7bbc9406bbcdb1dc70912a5ef.png)
![](https://i-blog.csdnimg.cn/direct/3a8ec8c7d5c648b491b79194019be923.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RobotController : MonoBehaviour
{
[Header("平滑过渡时间")]
[Range(0, 3)]
public float dampTime = 3f;
[Header("移动速度")]
public float speed = 3f;
[Header("转身速度")]
public float turnSpeed = 10f;
//虚拟轴
private float hor, ver;
//动画组件
private Animator ani;
//声音组件
private AudioSource aud;
[Header("喊叫声音片段")]
public AudioClip shoutClip;
//虚拟按键
private bool sneak,shout;
private void Awake()
{
ani = GetComponent<Animator>();
aud = GetComponent<AudioSource>();
}
private void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
//获取虚拟按键
sneak = Input.GetButton("Sneak");
shout = Input.GetButtonDown("Shout");
//if (sneak) {
// ani.SetBool("Sneak", sneak);
//}
//设置动画参数
ani.SetBool("Sneak", sneak);
if (shout) {
//触发参数shout
ani.SetTrigger("Shout");
//播放喊叫声音
AudioSource.PlayClipAtPoint(shoutClip,transform.position);
}
//只要按下一个方向键,就走
if (hor != 0 || ver != 0)
{
//设置Speed,角色动起来
ani.SetFloat("Speed", 5.6f, dampTime, Time.deltaTime);
//将向量转换成四元数
Quaternion targetQua = Quaternion.LookRotation(new Vector3(hor, 0, ver));
//lerp过去
transform.rotation = Quaternion.Lerp(transform.rotation, targetQua, Time.deltaTime * turnSpeed);
}
else
{
//停下来
ani.SetFloat("Speed", 0f);
}
//判断当前是否是Locomotion状态
bool isLocomotion = ani.GetCurrentAnimatorStateInfo(0).IsName("Locomotion");
if (isLocomotion)
{
//判断脚步声音是不是已经开始播放了
if (!aud.isPlaying)
{
//播放声音
aud.Play();
}
}
else
{
//停止声音
aud.Stop();
}
}
}
AudioListener挂载在角色身上
![](https://i-blog.csdnimg.cn/direct/c4274bf5150840a18279daf607752a89.png)
![](https://i-blog.csdnimg.cn/direct/ac65bac568f94f0c98a6ab85114b6d73.png)
![](https://i-blog.csdnimg.cn/direct/5c89151be200461c8051a686b99e02bf.png)
![](https://i-blog.csdnimg.cn/direct/d0a8c77f419548cb989effbd3d89a1d7.png)
参数不能超过1个
IK方向动力学
Inverse kinematics
反应的是一种由手部带到肩部的运动形式,在这个运动中,运动以手部这个自由端为起始,当手部进行运动时会自然的带动固定端肩部的运动。
![](https://i-blog.csdnimg.cn/direct/93bc72b6a99843b08c0157e327752579.png)
比如别人拉起你的手
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RobotController : MonoBehaviour
{
[Header("平滑过渡时间")]
[Range(0, 3)]
public float dampTime = 3f;
[Header("移动速度")]
public float speed = 3f;
[Header("转身速度")]
public float turnSpeed = 10f;
//虚拟轴
private float hor, ver;
//动画组件
private Animator ani;
//声音组件
private AudioSource aud;
[Header("喊叫声音片段")]
public AudioClip shoutClip;
//虚拟按键
private bool sneak,shout;
private void Awake()
{
ani = GetComponent<Animator>();
aud = GetComponent<AudioSource>();
}
private void Update()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
//获取虚拟按键
sneak = Input.GetButton("Sneak");
shout = Input.GetButtonDown("Shout");
//if (sneak) {
// ani.SetBool("Sneak", sneak);
//}
//设置动画参数
ani.SetBool("Sneak", sneak);
if (shout) {
//触发参数shout
ani.SetTrigger("Shout");
}
//只要按下一个方向键,就走
if (hor != 0 || ver != 0)
{
//设置Speed,角色动起来
ani.SetFloat("Speed", 5.6f, dampTime, Time.deltaTime);
//将向量转换成四元数
Quaternion targetQua = Quaternion.LookRotation(new Vector3(hor, 0, ver));
//lerp过去
transform.rotation = Quaternion.Lerp(transform.rotation, targetQua, Time.deltaTime * turnSpeed);
}
else
{
//停下来
ani.SetFloat("Speed", 0f);
}
//判断当前是否是Locomotion状态
bool isLocomotion = ani.GetCurrentAnimatorStateInfo(0).IsName("Locomotion");
if (isLocomotion)
{
//判断脚步声音是不是已经开始播放了
if (!aud.isPlaying)
{
//播放声音
aud.Play();
}
}
else
{
//停止声音
aud.Stop();
}
}
/// <summary>
/// 播放喊叫声音
/// </summary>
public void PlayShoutAudio(float a) {
Debug.Log(a);
//播放喊叫声音
AudioSource.PlayClipAtPoint(shoutClip, transform.position);
}
}
![](https://i-blog.csdnimg.cn/direct/1c0983f7e85b4683b838c363f91f35a6.png)
找到关节
![](https://i-blog.csdnimg.cn/direct/92d859d2cbde4e68836baa52ccf66df2.png)
![](https://i-blog.csdnimg.cn/direct/a17862c9b607477981c526e6a5a614f3.png)
![](https://i-blog.csdnimg.cn/direct/a638ee990c11425c85748dc25df62c8a.png)
![](https://i-blog.csdnimg.cn/direct/afc8126f0d6843de912fe5ba70021169.png)
![](https://i-blog.csdnimg.cn/direct/a1cfd077ed6f4cf2bdeaa8c866270197.png)
![](https://i-blog.csdnimg.cn/direct/edb1bf29022d413cbe4531c60dc2a492.png)
![](https://i-blog.csdnimg.cn/direct/be6a90c778a44f4d95e0bec9596a2eec.png)
![](https://i-blog.csdnimg.cn/direct/daebb24408f04e7d9455d538bfaa4ab2.png)
![](https://i-blog.csdnimg.cn/direct/a565af345353481387f33247ca08edcc.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EthanIKController : MonoBehaviour
{
private Animator ani;
[Header("IK动画的开关")]
public bool ikActive = false;
[Header("射击敌人的最大角度")]
public float fireMaxAngle = 100;
[Header("IK指向的目标")]
public Transform target;
[Header("IK指向的目标身高")]
public float targetHeight = 1.8f;
private void Awake()
{
ani = GetComponent<Animator>();
}
private void OnAnimatorIK(int layerIndex)
{
//获取玩家指向敌人的方向向量
Vector3 dir=target.position - transform.position;
//计算夹角
float angle=Vector3.Angle(dir, transform.forward);
if (angle < fireMaxAngle / 2)
{
ikActive = true;
}
else {
ikActive = false;
}
if (ikActive)
{
//设置IK权重
ani.SetIKPositionWeight(AvatarIKGoal.RightHand, 1);
//设置IK位置
ani.SetIKPosition(AvatarIKGoal.RightHand, target.position+Vector3.up*targetHeight);
//设置眼睛的IK权重
ani.SetLookAtWeight(1);
//设置眼睛的IK位置
ani.SetLookAtPosition(target.position+Vector3.up * targetHeight);
}
else {
//设置IK权重为0
ani.SetIKPositionWeight(AvatarIKGoal.RightHand, 0);
//设置眼睛权重为0
ani.SetLookAtWeight(0);
}
}
}
Curve
p341
![](https://i-blog.csdnimg.cn/direct/5829ab7fd2c34a2aa3a3bbbf2e21aeef.png)
![](https://i-blog.csdnimg.cn/direct/b5ee459957cf4af8923cfe14c1f7be94.png)
![](https://i-blog.csdnimg.cn/direct/3a8d2a3746c243a79afbbeae7e4ee85f.png)
![](https://i-blog.csdnimg.cn/direct/ed5f8264d39d4100a58b003656e51a5d.png)
![](https://i-blog.csdnimg.cn/direct/3aaaed309968476ba770f1b277a65889.png)
![](https://i-blog.csdnimg.cn/direct/b9c4c4e3b07843f69969ff629efc9fca.png)
绑定到一起只需要名称一致
p342
![](https://i-blog.csdnimg.cn/direct/7c1a2fd5ed004566b9259581bceacf15.png)
![](https://i-blog.csdnimg.cn/direct/6fc058e805a04a1db1836f9c5e8d2f4b.png)
![](https://i-blog.csdnimg.cn/direct/8dd8a2d3b8aa42b6b09074b0b2ba417f.png)
![](https://i-blog.csdnimg.cn/direct/3b46ab8531664a36b6e4540184ef84de.png)
![](https://i-blog.csdnimg.cn/direct/4de3b23db20a4667856c1842cdf48a0f.png)
![](https://i-blog.csdnimg.cn/direct/028219b6ec3f4172be919f5185c11814.png)
![](https://i-blog.csdnimg.cn/direct/6f4324898e3c45dda94ac0889f38de13.png)
![](https://i-blog.csdnimg.cn/direct/ce8affb668fc440b87c424950cc343ce.png)
![](https://i-blog.csdnimg.cn/direct/82b28015398244de97340359658501a4.png)
![](https://i-blog.csdnimg.cn/direct/2788e013e69e4d39a72084755b4e9c99.png)
开始慢后期快
![](https://i-blog.csdnimg.cn/direct/9e172ca7bef449ca9d5f0cae3060d351.png)