unity 脚部动画约束
动画播放方法
Play 方法
在Unity中,Play 方法用于播放动画,有以下两种重载形式:
function Play (mode : PlayMode = PlayMode.StopSameLayer) : bool
function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool
Play() 方法会开始播放指定名称为 animation 的动画,如果未指定动画名称,则播放默认动画。动画会立即开始播放,没有任何混合效果。
- 当
mode为PlayMode.StopSameLayer时,同一层的所有动画将停止播放。 - 当
mode为PlayMode.StopAll时,所有正在播放的动画都将停止。
如果动画已经在播放,其他动画会停止,但当前动画不会回到起始位置。若动画未设置为循环模式,播放结束后会停止并回到起始位置。如果无法播放动画(例如没有动画剪辑或者没有默认动画),Play() 方法将返回 false。
以下是使用示例:
// 播放默认动画
animation.Play();
// 播放walk动画 - 停止同一层的其他动画
animation.Play("walk");
// 播放walk动画 - 停止其他动画
animation.Play("walk", PlayMode.StopAll);
CrossFade 方法
CrossFade 方法用于在一定时间内实现动画的淡入淡出效果,其定义如下:
function CrossFade (animation : string, fadeLength : float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer) : void
该方法会在指定的 fadeLength 时间内淡入指定名称的动画,并淡出其他动画。
- 当
mode为PlayMode.StopSameLayer时,同一层的动画会在淡入指定动画时淡出。 - 当
mode为PlayMode.StopAll时,所有动画都会在淡入指定动画时淡出。
如果动画未设置为循环模式,播放完成后会停止并回到起始位置。
以下是使用示例:
// 淡入walk循环并且淡出同一层的所有其他动画,在0.2秒之内完成淡入淡出
animation.CrossFade("Walk", 0.2);
// 让一个角色包含Run和Idle动画,并且在玩家想移动的时候在它们之间淡入淡出
function Update () {
if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
animation.CrossFade("Run");
else
animation.CrossFade("Idle");
}
这里介绍了最常见的两种播放动画的方法,更多方法和属性可以查阅Unity的API文档。
人物简单控制脚本
以下是一个简单的人物控制脚本 RoleScriptContorl.cs,实现了人物的上下左右移动以及攻击功能:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
public static PlayerControl instance;
private AnimationClip animationClip;
private CharacterController controller;
public float runSpeed = 5;
private float moveSpeed = 3; // 走路速度
private string currentState = "";
private GameObject monster;
void Awake()
{
instance = this;
}
void Start ()
{
controller = GetComponent<CharacterController>();
print(transform.forward);
}
void Update ()
{
KeyboardControl();
}
void KeyboardControl()
{
Vector3 forward = transform.TransformDirection(Vector3.forward); // 从自身坐标转换为世界坐标
if(Input.GetKey(KeyCode.W))
{
if(Input.GetKey(KeyCode.LeftShift))
{
currentState = RoleAnimationState.RUN; // 改变人物动画状态
PlayAnimation(currentState); // 播放动画
controller.SimpleMove(forward * runSpeed * Time.deltaTime * 100);
}
else
{
currentState = RoleAnimationState.WALK;
PlayAnimation(currentState);
controller.SimpleMove(forward * moveSpeed * Time.deltaTime * 100);
}
}
if (Input.GetKey(KeyCode.S))
{
Vector3 back = transform.TransformDirection(Vector3.back);
currentState = RoleAnimationState.WALK;
PlayAnimation(currentState);
controller.SimpleMove(back * moveSpeed * Time.deltaTime * 100);
}
if (Input.GetKey(KeyCode.A))
{
Vector3 up = transform.TransformDirection(Vector3.up);
transform.Rotate(Vector3.up * -50 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
Vector3 up = transform.TransformDirection(Vector3.up);
transform.Rotate(Vector3.up * 50 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.J))
{
currentState = RoleAnimationState.ATTACK;
PlayAnimation(RoleAnimationState.ATTACK);
}
if (Input.GetKey(KeyCode.K))
{
currentState = RoleAnimationState.ATTACK01;
PlayAnimation(RoleAnimationState.ATTACK01);
}
if(currentState != RoleAnimationState.IDLE && !animation.IsPlaying(currentState))
{
PlayAnimation(RoleAnimationState.IDLE);
}
}
void PlayAnimation(string name)
{
currentState = name;
animationClip = animation[name].clip;
gameObject.animation.CrossFade(animationClip.name);
}
}
动画状态脚本类
以下是一个定义动画状态的脚本类:
using UnityEngine;
using System.Collections;
public class RoleAnimationState : MonoBehaviour
{
public const string IDLE = "Idle";
public const string WALK = "Walk";
public const string RUN = "Run";
public const string ATTACK = "Attack";
public const string ATTACK01 = "Attack01";
}
将上述代码直接挂载到人物模型上即可使用。同时,你可以查看人物模型写真和Inspector的组件内容来进一步了解和调整相关设置。