unity3d简单的角色控制

2015年02月02日 14:20 0 点赞 0 评论 更新于 2025-11-21 16:00

本文将详细介绍Unity3D中简单角色控制在脚步方面的内容,包括动画播放方法和角色控制脚本,同时给出相应的代码和教程。

动画播放方法

1. Play() 方法

在Unity中,Play() 方法用于开始播放动画。其函数定义如下:

function Play (mode: PlayMode = PlayMode.StopSameLayer) : bool
function Play (animation : string, mode : PlayMode = PlayMode.StopSameLayer) : bool
  • Play() 方法会开始播放名称为 animation 的动画,若未指定动画名称,则播放默认动画。动画会突然开始播放,没有任何混合效果。
  • modePlayMode.StopSameLayer 时,所有在同一层的动画将停止播放;当 modePlayMode.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
  • 如果 modePlayMode.StopSameLayer,在同一层的动画将在指定动画淡入时淡出;若 modePlayMode.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,包括上下左右移动以及 JK 键攻击的功能。

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))
{
transform.Rotate(Vector3.up * -50 * Time.deltaTime);
}

if (Input.GetKey(KeyCode.D))
{
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的组件内容进行进一步调试。