unity控制人物脚步

2015年01月12日 15:49 0 点赞 0 评论 更新于 2025-11-21 14:06

一、Unity3D中Animation的常见属性及方法

1. Play 方法

在Unity3D里,Play 方法可用于播放动画,其有两种重载形式:

function Play (mode : PlayMode = PlayMode.StopSameLayer) : bool
function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool
  • Play() 方法会开始播放名称为 animation 的动画,若未指定动画名称,则播放默认动画。动画会直接开始播放,没有任何混合效果。
  • 当模式为 PlayMode.StopSameLayer 时,同一层的所有动画将停止播放;若模式为 PlayMode.StopAll,则所有正在播放的动画都会停止。
  • 如果动画已经在播放,其他动画会停止,但当前动画不会回退到开始位置。
  • 若动画未设置为循环模式,播放结束后它将停止并回退到开始位置。
  • 若动画无法播放(如没有动画剪辑或没有默认动画),Play() 方法将返回 false

以下是使用示例:

// 播放默认动画
animation.Play();

// 播放walk动画 - 停止同一层的其他动画
animation.Play("walk");

// 播放walk动画 - 停止其他动画
animation.Play("walk", PlayMode.StopAll);

2. CrossFade 方法

CrossFade 方法用于在一定时间内实现动画的淡入淡出效果,其定义如下:

function CrossFade (animation : string, fadeLength : float = 0.3F, mode : PlayMode = PlayMode.StopSameLayer) : void

该方法会在指定时间 fadeLength 内淡入名称为 animation 的动画,并淡出其他动画。当模式为 PlayMode.StopSameLayer 时,同一层的动画会在淡入时淡出;若模式为 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文档。

二、人物简单控制脚步脚本

下面是一个实现人物简单控制脚步的脚本,包括上下左右移动,以及 JK 键攻击功能。

1. 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);
}
}

2. 动画状态脚本类

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";
}

将上述代码直接挂载到人物模型上即可使用。

三、人物模型共享

为方便大家学习,这里将人物模型共享给大家,脚本代码上面已给出,不再重复。 模型网盘链接:http://pan.baidu.com/s/1kThy8Gf

作者信息

feifeila

feifeila

共发布了 3994 篇文章