fjkrl 发表于 2013-3-4 22:56

Unity3D 第五课 如何控制播放骨骼动画,让你的人物奔跑吧

Unity3D 第五课 如何控制播放骨骼动画,让你的人物奔跑吧

这一课我将介绍如何控制播放骨骼动画:
因为没有模型,我就用引擎里自带的了
这个模型了
引擎自带的这个模型有这几个动画那么我们怎么开获取这个动画来控制呢 ?

我们要用到:
[*]//获取这个动画并播放
[*]animation.Play("idle");

复制代码
那么怎么把控制这个人物所有动画呢?
现在就要用到之前写的第4课了,如果大家都有认真看过之前4课的教程的的话就很简单了…
在之前的代码中加入
[*]    //站立状态
[*]    public const int PLAY_IDE = 4;

复制代码
这样我们就有了人物的五个状态,上下左右,站立…
初始化状态为
[*]      void Awake ()
[*]      {
[*]//初始化为站立状态
[*]                gameState = PLAY_IDE;
[*]      }

复制代码
好了下面贴出源码:
[*]using UnityEngine;
[*]using System.Collections;
[*]
[*]public class PlayController: MonoBehaviour
[*]{
[*]
[*]      //人物行走方向
[*]      public const int PLAY_UP = 0;
[*]      public const int PLAY_RIGHT = 1;
[*]      public const int PLAY_DOWN = 2;
[*]      public const int PLAY_LEFT = 3;
[*]      //站立状态
[*]      public const int PLAY_IDE = 4;
[*]
[*]      //记录当前人物的状态
[*]      private int gameState = 0;
[*]
[*]      //人物移动的速度
[*]      private int moveSpeed = 10;
[*]      void Awake ()
[*]      {
[*]                gameState = PLAY_IDE;
[*]      }
[*]
[*]      void Update ()
[*]      {
[*]                KeyMover();
[*]      }
[*]
[*]      void KeyMover()
[*]      {
[*]                if(Input.GetKey(KeyCode.A))
[*]                {
[*]                        setGameState(PLAY_LEFT);
[*]                }
[*]                else if(Input.GetKey(KeyCode.D))
[*]                {
[*]                        setGameState(PLAY_RIGHT);
[*]                }
[*]                else if(Input.GetKey(KeyCode.S))
[*]                {
[*]                        setGameState(PLAY_DOWN);
[*]                }
[*]                else if(Input.GetKey(KeyCode.W))
[*]                {
[*]                        setGameState(PLAY_UP);
[*]                }
[*]                else if(Input.GetKey(KeyCode.Space))
[*]                {
[*]
[*]                }
[*]                else
[*]                {
[*]                        //松开按键播放的动画
[*]                        setGameState(PLAY_IDE);
[*]                }
[*]      }
[*]
[*]         public void setGameState(int newState)
[*]      {
[*]                //根据当前模型方向一上一次备份的方向计算出模型的角度
[*]                int rotareValue = (newState - gameState) * 90;
[*]                print(rotareValue);
[*]                Vector3 transFormValue = new Vector3();
[*]                //播放行走动画
[*]                animation.Play("walk");
[*]                //模型移动的数值
[*]                switch(newState)
[*]                {
[*]                        case PLAY_UP:
[*]                              transFormValue = Vector3.forward * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_DOWN:
[*]                              transFormValue = (-Vector3.forward) * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_LEFT:
[*]                              transFormValue = Vector3.left * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_RIGHT:
[*]                              transFormValue = (-Vector3.left) * Time.deltaTime;
[*]                              break;
[*]                        case PLAY_IDE:
[*]                              animation.Play("idle");
[*]                              break;
[*]                }
[*]                //模型旋转
[*]                transform.Rotate(Vector3.up ,rotareValue);
[*]                //移动人物
[*]                transform.Translate(transFormValue * moveSpeed,Space.World);
[*]                gameState = newState;
[*]      }
[*]}

复制代码




HPhaSHIqi 发表于 2013-6-22 15:54


不错 不错 不错{:soso__3922851084632044791_6:}

难得糊督 发表于 2013-6-25 21:18

感谢分享..

灵之舞 发表于 2017-2-7 21:31

楼主是超人

Mr.菟 发表于 2017-2-7 21:58

好帖就是要顶

sunshin 发表于 2017-2-7 21:47

很好哦

tinggu 发表于 2017-2-7 21:54

不错不错

辣条 发表于 2017-2-7 21:56

LZ真是人才

wai2dance 发表于 2017-2-13 20:45

很不错

转角 发表于 2017-2-13 21:02

顶顶多好
页: [1]
查看完整版本: Unity3D 第五课 如何控制播放骨骼动画,让你的人物奔跑吧