Unity3D 示例——人物移动

约定不等于承诺〃 2021-11-09 00:30 501阅读 0赞

一个是通过W、A、S、D来移动人物(示例一),另个是按屏幕上的按钮来移动人物(示例二)。很简单,只改了几行代码。

下面是“Assets”文件夹里面的资源。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTg4MDAz_size_16_color_FFFFFF_t_70

#

示例一:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class E3_07keyboard : MonoBehaviour
  5. {
  6. //动画数组
  7. private Object[] animUp;
  8. private Object[] animDown;
  9. private Object[] animLeft;
  10. private Object[] animRight;
  11. //地图贴图
  12. private Texture2D map;
  13. //当前人物动画
  14. private Object[] tex;
  15. //人物X坐标
  16. private int x;
  17. //人物Y坐标
  18. private int y;
  19. //帧序列
  20. private int nowFram;
  21. //动画帧的总数
  22. private int mFrameCount;
  23. //限制一秒多少帧
  24. private float fps = 5;
  25. //限制帧的时间
  26. private float time = 0;
  27. void Start()
  28. {
  29. //得到帧动画中的所有图片资源
  30. animUp = Resources.LoadAll("up");
  31. animDown = Resources.LoadAll("down");
  32. animLeft = Resources.LoadAll("left");
  33. animRight = Resources.LoadAll("right");
  34. //得到地图资源
  35. map = (Texture2D)Resources.Load("map/map");
  36. //设置默认动画
  37. tex = animUp;
  38. }
  39. void OnGUI()
  40. {
  41. //绘制贴图
  42. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0);
  43. //绘制帧动画
  44. DrawAnimation(tex, new Rect(x, y, 32, 48));
  45. //点击按钮移动人物
  46. if (Input.GetKey(KeyCode.W))
  47. {
  48. y -= 2;
  49. tex = animUp;
  50. }
  51. if (Input.GetKey(KeyCode.S))
  52. {
  53. y += 2;
  54. tex = animDown;
  55. }
  56. if (Input.GetKey(KeyCode.A))
  57. {
  58. x -= 2;
  59. tex = animLeft;
  60. }
  61. if (Input.GetKey(KeyCode.D))
  62. {
  63. x += 2;
  64. tex = animRight;
  65. }
  66. }
  67. void DrawAnimation(Object[] tex, Rect rect)
  68. {
  69. //绘制当前帧
  70. GUI.DrawTexture(rect, (Texture)tex[nowFram], ScaleMode.StretchToFill, true, 0);
  71. //计算限制帧时间
  72. time += Time.deltaTime;
  73. //超过限制帧则切换图片
  74. if (time >= 1.0 / fps)
  75. {
  76. //帧序列切换
  77. nowFram++;
  78. //限制帧清空
  79. time = 0;
  80. //超过帧动画总数从第0帧开始
  81. if (nowFram >= tex.Length)
  82. {
  83. nowFram = 0;
  84. }
  85. }
  86. }
  87. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTg4MDAz_size_16_color_FFFFFF_t_70 1

示例二

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class E3_07button : MonoBehaviour
  5. {
  6. //动画数组
  7. private Object[] animUp;
  8. private Object[] animDown;
  9. private Object[] animLeft;
  10. private Object[] animRight;
  11. //地图贴图
  12. private Texture2D map;
  13. //当前人物动画
  14. private Object[] tex;
  15. //人物X坐标
  16. private int x;
  17. //人物Y坐标
  18. private int y;
  19. //帧序列
  20. private int nowFram;
  21. //动画帧的总数
  22. private int mFrameCount;
  23. //限制一秒多少帧
  24. private float fps = 5;
  25. //限制帧的时间
  26. private float time = 0;
  27. void Start()
  28. {
  29. //得到帧动画中的所有图片资源
  30. animUp = Resources.LoadAll("up");
  31. animDown = Resources.LoadAll("down");
  32. animLeft = Resources.LoadAll("left");
  33. animRight = Resources.LoadAll("right");
  34. //得到地图资源
  35. map = (Texture2D)Resources.Load("map/map");
  36. //设置默认动画
  37. tex = animUp;
  38. }
  39. void OnGUI()
  40. {
  41. //绘制贴图
  42. GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), map, ScaleMode.StretchToFill, true, 0);
  43. //绘制帧动画
  44. DrawAnimation(tex, new Rect(x, y, 32, 48));
  45. //点击按钮移动人物
  46. if (GUILayout.RepeatButton("向上"))
  47. {
  48. y -= 2;
  49. tex = animUp;
  50. }
  51. if (GUILayout.RepeatButton("向下"))
  52. {
  53. y += 2;
  54. tex = animDown;
  55. }
  56. if (GUILayout.RepeatButton("向左"))
  57. {
  58. x -= 2;
  59. tex = animLeft;
  60. }
  61. if (GUILayout.RepeatButton("向右"))
  62. {
  63. x += 2;
  64. tex = animRight;
  65. }
  66. }
  67. void DrawAnimation(Object[] tex, Rect rect)
  68. {
  69. //绘制当前帧
  70. GUI.DrawTexture(rect, (Texture)tex[nowFram], ScaleMode.StretchToFill, true, 0);
  71. //计算限制帧时间
  72. time += Time.deltaTime;
  73. //超过限制帧则切换图片
  74. if (time >= 1.0 / fps)
  75. {
  76. //帧序列切换
  77. nowFram++;
  78. //限制帧清空
  79. time = 0;
  80. //超过帧动画总数从第0帧开始
  81. if (nowFram >= tex.Length)
  82. {
  83. nowFram = 0;
  84. }
  85. }
  86. }
  87. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTg4MDAz_size_16_color_FFFFFF_t_70 2

发表评论

表情:
评论列表 (有 0 条评论,501人围观)

还没有评论,来说两句吧...

相关阅读

    相关 unity3D人物移动的实现(二)

    运行之前的项目,小人可以自由移动,但是移动过程中,会有停止按键后滑动一段距离的效果,这是因为脚本中的GetAxis函数,他得到的值随着按键的过程而变化,所以每一次松开的时候,该