http://blog.csdn.net/xys289187120/article/details/6961080

       3D 世界中自定义模型的使用恐怕是重中之重,因为系统自身提供的模型肯定是无法满足GD对游戏的策划,所以为了让游戏更加绚丽,我们须要调用美术制作的精品模型与动画,本章MOMO将带领盆友们学习Unity3D中模型的载入与动画的播放,哇咔咔~~

       由于MOMO手头上没有现成的模型,所以我将在Unity3D 官网中下载官方提供的游戏DEMO 中的模型来使用。另外官方提供了很多Unity3D 游戏DEMO,与详细的文档。可以帮助我们学习Unity.有兴趣的盆友可以去看看哈。

下载页面:http://unity3d.com/support/resources/  

本章博文的目的是利用上一章介绍的游戏摇杆来控制人物模型的移动,与行走动画的播放。

 

 


如上图所示Create中的文件夹male中存放着模型动画与贴图等,这个应该是美术提供给我们的。然后将整个male用鼠标拖动到左侧3D世界中,通过移动,旋转,缩放将人物模型放置在一个理想的位置。右侧红框内设置模型动画的属性。

Animation  

        idle1  该模型默认动画名称为idle1

Animations 

        size   该模型动画的数量

        Element 该模型的动画名称

Play Automatically 是否自动播放

Animation Physics 是否设置该模型物理碰撞

Animation Only if Visable 是否设置该模型仅自己显示

给该模型绑定一个脚本Controller.cs 用来接收摇杆返回的信息更新模型动画。

Controller.cs

[csharp] view plaincopy
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Controller : MonoBehaviour {
  4. //人物的行走方向状态
  5. public const int HERO_UP= 0;
  6. public const int HERO_RIGHT= 1;
  7. public const int HERO_DOWN= 2;
  8. public const int HERO_LEFT= 3;
  9. //人物当前行走方向状态
  10. public int state = 0;
  11. //备份上一次人物当前行走方向状态
  12. //这里暂时没有用到
  13. public int backState = 0;
  14. //游戏摇杆对象
  15. public MPJoystick moveJoystick;
  16. //这个方法只调用一次,在Start方法之前调用
  17. public void Awake() {
  18. }
  19. //这个方法只调用一次,在Awake方法之后调用
  20. void Start () {
  21. state = HERO_DOWN;
  22. }
  23. void Update () {
  24. //获取摇杆控制的方向数据 上一章有详细介绍
  25. float touchKey_x =  moveJoystick.position.x;
  26. float touchKey_y =  moveJoystick.position.y;
  27. if(touchKey_x == -1){
  28. setHeroState(HERO_LEFT);
  29. }else if(touchKey_x == 1){
  30. setHeroState(HERO_RIGHT);
  31. }
  32. if(touchKey_y == -1){
  33. setHeroState(HERO_DOWN);
  34. }else if(touchKey_y == 1){
  35. setHeroState(HERO_UP);
  36. }
  37. if(touchKey_x == 0 && touchKey_y ==0){
  38. //松开摇杆后播放默认动画,
  39. //不穿参数为播放默认动画。
  40. animation.Play();
  41. }
  42. }
  43. public void setHeroState(int newState)
  44. {
  45. //根据当前人物方向 与上一次备份方向计算出模型旋转的角度
  46. int rotateValue = (newState - state) * 90;
  47. Vector3 transformValue = new Vector3();
  48. //播放行走动画
  49. animation.Play("walk");
  50. //模型移动的位移的数值
  51. switch(newState){
  52. case HERO_UP:
  53. transformValue = Vector3.forward * Time.deltaTime;
  54. break;
  55. case HERO_DOWN:
  56. transformValue = -Vector3.forward * Time.deltaTime;
  57. break;
  58. case HERO_LEFT:
  59. transformValue = Vector3.left * Time.deltaTime;
  60. break;
  61. case HERO_RIGHT:
  62. transformValue = -Vector3.left * Time.deltaTime;
  63. break;
  64. }
  65. //模型旋转
  66. transform.Rotate(Vector3.up, rotateValue);
  67. //模型移动
  68. transform.Translate(transformValue, Space.World);
  69. backState = state;
  70. state = newState;
  71. }
  72. }

 

上一章介绍了javaScript脚本使用游戏摇杆的方法,本章MOMO告诉大家使用C#脚本来使用游戏摇杆,上面我用 Controller.cs  C#脚本来接收系统提供的Joystick.js是肯定无法使用的,须要修改成.cs文件,我在国外的一个网站上看到了一个老外帮我们已经修改了,那么我将他修改后的代码贴出来方便大家学习,有兴趣的朋友可以研究研究。哇咔咔~

MPJoystick.cs

 

[csharp] view plaincopy
  1. using UnityEngine;
  2. /**
  3. * File: MPJoystick.cs
  4. * Author: Chris Danielson of (monkeyprism.com)
  5. *
  6. // USED TO BE: Joystick.js taken from Penelope iPhone Tutorial
  7. //
  8. // Joystick creates a movable joystick (via GUITexture) that
  9. // handles touch input, taps, and phases. Dead zones can control
  10. // where the joystick input gets picked up and can be normalized.
  11. //
  12. // Optionally, you can enable the touchPad property from the editor
  13. // to treat this Joystick as a TouchPad. A TouchPad allows the finger
  14. // to touch down at any point and it tracks the movement relatively
  15. // without moving the graphic
  16. */
  17. [RequireComponent(typeof(GUITexture))]
  18. public class MPJoystick : MonoBehaviour
  19. {
  20. class Boundary {
  21. public Vector2 min = Vector2.zero;
  22. public Vector2 max = Vector2.zero;
  23. }
  24. private static MPJoystick[] joysticks;   // A static collection of all joysticks
  25. private static bool enumeratedJoysticks = false;
  26. private static float tapTimeDelta = 0.3f;    // Time allowed between taps
  27. public bool touchPad;
  28. public Vector2 position = Vector2.zero;
  29. public Rect touchZone;
  30. public Vector2 deadZone = Vector2.zero;  // Control when position is output
  31. public bool normalize = false; // Normalize output after the dead-zone?
  32. public int tapCount;
  33. private int lastFingerId = -1;   // Finger last used for this joystick
  34. private float tapTimeWindow;     // How much time there is left for a tap to occur
  35. private Vector2 fingerDownPos;
  36. //private float fingerDownTime;
  37. //private float firstDeltaTime = 0.5f;
  38. private GUITexture gui;
  39. private Rect defaultRect;    // Default position / extents of the joystick graphic
  40. private Boundary guiBoundary = new Boundary();   // Boundary for joystick graphic
  41. private Vector2 guiTouchOffset;  // Offset to apply to touch input
  42. private Vector2 guiCenter;   // Center of joystick
  43. void Start() {
  44. gui = (GUITexture)GetComponent(typeof(GUITexture));
  45. defaultRect = gui.pixelInset;
  46. defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5;
  47. defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
  48. transform.position = Vector3.zero;
  49. if (touchPad) {
  50. // If a texture has been assigned, then use the rect ferom the gui as our touchZone
  51. if ( gui.texture )
  52. touchZone = defaultRect;
  53. } else {
  54. guiTouchOffset.x = defaultRect.width * 0.5f;
  55. guiTouchOffset.y = defaultRect.height * 0.5f;
  56. // Cache the center of the GUI, since it doesn't change
  57. guiCenter.x = defaultRect.x + guiTouchOffset.x;
  58. guiCenter.y = defaultRect.y + guiTouchOffset.y;
  59. // Let's build the GUI boundary, so we can clamp joystick movement
  60. guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
  61. guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
  62. guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
  63. guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
  64. }
  65. }
  66. public Vector2 getGUICenter() {
  67. return guiCenter;
  68. }
  69. void Disable() {
  70. gameObject.active = false;
  71. //enumeratedJoysticks = false;
  72. }
  73. private void ResetJoystick() {
  74. gui.pixelInset = defaultRect;
  75. lastFingerId = -1;
  76. position = Vector2.zero;
  77. fingerDownPos = Vector2.zero;
  78. }
  79. private bool IsFingerDown() {
  80. return (lastFingerId != -1);
  81. }
  82. public void LatchedFinger(int fingerId) {
  83. // If another joystick has latched this finger, then we must release it
  84. if ( lastFingerId == fingerId )
  85. ResetJoystick();
  86. }
  87. void Update() {
  88. if (!enumeratedJoysticks) {
  89. // Collect all joysticks in the game, so we can relay finger latching messages
  90. joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick));
  91. enumeratedJoysticks = true;
  92. }
  93. int count = Input.touchCount;
  94. if ( tapTimeWindow > 0 )
  95. tapTimeWindow -= Time.deltaTime;
  96. else
  97. tapCount = 0;
  98. if ( count == 0 )
  99. ResetJoystick();
  100. else
  101. {
  102. for(int i = 0; i < count; i++) {
  103. Touch touch = Input.GetTouch(i);
  104. Vector2 guiTouchPos = touch.position - guiTouchOffset;
  105. bool shouldLatchFinger = false;
  106. if (touchPad) {
  107. if (touchZone.Contains(touch.position))
  108. shouldLatchFinger = true;
  109. }
  110. else if (gui.HitTest(touch.position)) {
  111. shouldLatchFinger = true;
  112. }
  113. // Latch the finger if this is a new touch
  114. if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId )) {
  115. if (touchPad) {
  116. //gui.color.a = 0.15;
  117. lastFingerId = touch.fingerId;
  118. //fingerDownPos = touch.position;
  119. //fingerDownTime = Time.time;
  120. }
  121. lastFingerId = touch.fingerId;
  122. // Accumulate taps if it is within the time window
  123. if ( tapTimeWindow > 0 )
  124. tapCount++;
  125. else {
  126. tapCount = 1;
  127. tapTimeWindow = tapTimeDelta;
  128. }
  129. // Tell other joysticks we've latched this finger
  130. //for (  j : Joystick in joysticks )
  131. foreach (MPJoystick j in joysticks) {
  132. if (j != this)
  133. j.LatchedFinger( touch.fingerId );
  134. }
  135. }
  136. if ( lastFingerId == touch.fingerId ) {
  137. // Override the tap count with what the iPhone SDK reports if it is greater
  138. // This is a workaround, since the iPhone SDK does not currently track taps
  139. // for multiple touches
  140. if ( touch.tapCount > tapCount )
  141. tapCount = touch.tapCount;
  142. if ( touchPad ) {
  143. // For a touchpad, let's just set the position directly based on distance from initial touchdown
  144. position.x = Mathf.Clamp( ( touch.position.x - fingerDownPos.x ) / ( touchZone.width / 2 ), -1, 1 );
  145. position.y = Mathf.Clamp( ( touch.position.y - fingerDownPos.y ) / ( touchZone.height / 2 ), -1, 1 );
  146. } else {
  147. // Change the location of the joystick graphic to match where the touch is
  148. Rect r = gui.pixelInset;
  149. r.x =  Mathf.Clamp( guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x );
  150. r.y =  Mathf.Clamp( guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y );
  151. gui.pixelInset = r;
  152. }
  153. if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  154. ResetJoystick();
  155. }
  156. }
  157. }
  158. if (!touchPad) {
  159. // Get a value between -1 and 1 based on the joystick graphic location
  160. position.x = ( gui.pixelInset.x + guiTouchOffset.x - guiCenter.x ) / guiTouchOffset.x;
  161. position.y = ( gui.pixelInset.y + guiTouchOffset.y - guiCenter.y ) / guiTouchOffset.y;
  162. }
  163. // Adjust for dead zone
  164. var absoluteX = Mathf.Abs( position.x );
  165. var absoluteY = Mathf.Abs( position.y );
  166. if (absoluteX < deadZone.x) {
  167. // Report the joystick as being at the center if it is within the dead zone
  168. position.x = 0;
  169. }
  170. else if (normalize) {
  171. // Rescale the output after taking the dead zone into account
  172. position.x = Mathf.Sign( position.x ) * ( absoluteX - deadZone.x ) / ( 1 - deadZone.x );
  173. }
  174. if (absoluteY < deadZone.y) {
  175. // Report the joystick as being at the center if it is within the dead zone
  176. position.y = 0;
  177. }
  178. else if (normalize) {
  179. // Rescale the output after taking the dead zone into account
  180. position.y = Mathf.Sign( position.y ) * ( absoluteY - deadZone.y ) / ( 1 - deadZone.y );
  181. }
  182. }
  183. }

 

导出 build and run  看看在iPhone 上的效果,通过触摸游戏摇杆可以控制人物的上,下,左,右 ,左上,右上,左下,右下 8个方向的移动啦,不错吧,哇咔咔~~

 

 


 

 

 

 


 

 

最后欢迎各位盆友可以和MOMO一起讨论Unity3D游戏开发,本来昨天就想发表这篇文章,结果晚上去打高尔夫球连挥N杆,打的回家后浑身酸痛,回家就睡觉啦~希望大家在学习的同时别忘了多运动。哇咔咔~~~ 附上Unity3D工程的下载地址,Xcode项目我就不上传了,须要的自己导出。

 

转载于:https://www.cnblogs.com/songtzu/archive/2013/04/09/3009551.html

Unity3D 游戏引擎之FBX模型的载入与人物行走动画的播放【转】相关推荐

  1. Unity3D 游戏引擎之FBX模型的载入与人物行走动画的播放(十二)

    Unity3D 游戏引擎之FBX模型的载入与人物行走动画的播放 雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong. ...

  2. Unity3D研究院之FBX模型的载入与人物行走动画的播放(十二)

     3D 世界中自定义模型的使用恐怕是重中之重,因为系统自身提供的模型肯定是无法满足GD对游戏的策划,所以为了让游戏更加绚丽,我们须要调用美术制作的精品模型与动画,本章MOMO将带领盆友们学习Unity ...

  3. Unity3D 游戏引擎之脚本实现模型的平移与旋转(六)

    Unity3D 游戏引擎之脚本实现模型的平移与旋转 雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/ar ...

  4. Unity3D 游戏引擎之IOS高级界面发送消息与Unity3D消息的接收(九)

    Unity3D 游戏引擎之IOS高级界面发送消息与Unity3D消息的接收 雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原文地址:http://blog.csdn.net/xys289 ...

  5. Unity3D 游戏引擎之平面小球重力感应详解【转】

    http://blog.csdn.net/xys289187120/article/details/6969333       手机重力感应应该对大多数开发者并不陌生,在新一代智能手机Android  ...

  6. unity3d 剧情制作_游戏设计作品集干货:跨专业如何入门 Unity3D 游戏引擎?

    游戏设计作品集,涉及游戏策划与玩法.游戏美术与Unity3D游戏引擎等内容的学习. 之前克瑞斯介绍过了游戏策划与游戏美术,本期再详细聊下什么是Unity3D游戏引擎,以及学习Unity3D游戏引擎的详 ...

  7. Unity3D 游戏引擎之构建简单的游戏世界(三)

    Unity3D 游戏引擎之构建简单的游戏世界 雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/archi ...

  8. Unity3D游戏引擎之构建游戏框架与导出IOS项目(一)

    Unity3D游戏引擎之构建游戏框架与导出IOS项目 雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/a ...

  9. 我们月光工作室和我们的3款游戏被国内Unity3D游戏引擎第一技术论坛--“游戏蛮牛” 报道啦!...

    注明:目前月光工作室已经停止更新,因为创业伙伴回国,小编自己忙于<程序员联盟>社区的各样事务.以后也许会重拾App开发吧.谨以此文纪念那段美好的时光!以下的某些链接也已失效. 我们月光工作 ...

最新文章

  1. 【开发环境】安装 Visual Studio Code 开发环境 ( 下载 Visual Studio Code 安装器 | Visual Studio Code )
  2. 批量更改Windows操作系统文件名
  3. python通过ftp上传文本文件storlines怎么用_用python将本地文件上传到FTP报错
  4. rocketmq 消息 自定义_RocketMQ消息轨迹-设计篇
  5. C++Primer学习笔记:第7章 类
  6. 使用Linux的alternatives命令替换选择软件的版本
  7. 3. 机器学习中为什么需要梯度下降_【干货】机器学习 | 为什么机器能“学习”?——感知器和梯度下降...
  8. poj1200 Crazy Search(hash)
  9. 应用HTK搭建语音拨号系统2:创建单音素HMM模型
  10. synchronized工作原理剖析(一)
  11. 芯片(架构)顶会截稿时间和开会时间记录(ISSCC、VLSI、ISCA、HPCA、MICRO、DAC等)
  12. HDU1728 BFS
  13. Windows平台好用但小众软件推荐
  14. 网页中插入FLASH的三种方法
  15. 计划扑克(Planning Poker)
  16. 网络协议 11 - Socket 编程(下):眼见为实耳听为虚
  17. plsql快速导入sql文件
  18. Varint+ZigZag编码和解码
  19. Python 之 小白爬虫
  20. 「 Luogu P2657 」 windy数

热门文章

  1. 火车车次查询-余票查询--Api接口
  2. 公司年会文化片主题和方案设计
  3. wps中将文档输出为pdf_如何将wps文档转换为pdf格式
  4. Macbook 强制关机快捷键
  5. Scrapy爬虫之热门网站数据爬取--------第2关
  6. C语言绘图示例-分色调图20例
  7. 数据分析师+前途无忧爬虫分析
  8. U盘损坏和文件修复(前方高能!!!)
  9. 青春的记忆(原创纪实小说)
  10. 【爆料】我是一个APP开发者:为啥会有打包党和上传手机号