当前位置:   article > 正文

Unity5.x制作合金弹头(二)-主角的创建_unity制作合金弹头

unity制作合金弹头

在前面介绍了开始界面的创建点击打开链接

在我们创建完主游戏界面的背景后接下来开始完善主角的创建

一.主角的动作

选中Hierarchy窗口中的主角,点中Window->Animation的Create将自动为该对象添加Animator组件

将每帧图片按需要的时间帧拖过去


在Animator窗口内将增加一个动画


接下来点击如下图的加号,可以选择一个类型,这里选择Bool类型


点击idle动画与walk动画的连线

按如下图选择,意思是当触发变量walk为true时,动作walk将播放

动画管理代码列表如下:

AnimController.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. public class AnimController : MonoBehaviour
  4. {
  5. public Animator[] anim;
  6. public void PlayIdleAnimation()
  7. {
  8. foreach(Animator value in anim)
  9. {
  10. value.SetBool("Walk",false);
  11. }
  12. anim [0].SetBool ("Jump",false);
  13. anim [0].SetBool ("ShootForward",false);
  14. anim [0].SetBool ("ShootUP",false);
  15. }
  16. public void PlayWalkAnimation()
  17. {
  18. foreach (Animator value in anim)
  19. {
  20. value.SetBool("Walk",true);
  21. }
  22. }
  23. public void PlayJumpAnimation()
  24. {
  25. foreach (Animator value in anim)
  26. {
  27. value.SetTrigger("Jump");
  28. }
  29. }
  30. public void PlayTrowAnimation()
  31. {
  32. anim [0].SetTrigger ("Throw");
  33. }
  34. public void PlayAttackAnimation()
  35. {
  36. anim [0].SetTrigger ("Attack");
  37. }
  38. public void PlayShootForwardAnimation()
  39. {
  40. anim [0].SetBool ("ShootForward",true);
  41. }
  42. public void PlayShootUpAnimation()
  43. {
  44. anim [0].SetBool ("ShootUP",true);
  45. }
  46. }



二.主角的移动管理

给主角添加BoxCollider2D,RigidBody2D,AudioSource组件,以及AnimControlle C#r脚本组件

PlayerController.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. public enum BulletDirection
  4. {
  5. RIGHT,LEFT
  6. }
  7. public class PlayController : MonoBehaviour
  8. {
  9. private Rigidbody2D rigidBody;
  10. private AnimController anim;
  11. private ShootAnim shoot;
  12. private GrenadeAnim grenade;
  13. //给钢体一个移动的速度
  14. public float moveSpeed=4.0f;
  15. //给钢体起跳的力
  16. public float upForce=700.0f;
  17. public BulletDirection bdt=BulletDirection.RIGHT;
  18. // Use this for initialization
  19. void Start ()
  20. {
  21. rigidBody =GetComponent<Rigidbody2D> ();
  22. anim = GetComponent<AnimController> ();
  23. shoot = GetComponent<ShootAnim> ();
  24. grenade = GetComponent<GrenadeAnim> ();
  25. }
  26. // Update is called once per frame
  27. void Update ()
  28. {
  29. #region idle状态
  30. rigidBody.velocity=new Vector2(0,rigidBody.velocity.y);
  31. anim.PlayIdleAnimation();
  32. #endregion
  33. #region 控制移动
  34. //获取按键的值,当h小于0时,表示按下A;h值大于0时,表示按下D
  35. float h = Input.GetAxis ("Horizontal");
  36. if (h>0)
  37. {
  38. bdt=BulletDirection.RIGHT;
  39. this.transform.rotation=Quaternion.Euler(new Vector3(0,180,0));
  40. rigidBody.velocity=new Vector2(h*moveSpeed,rigidBody.velocity.y);
  41. anim.PlayWalkAnimation();
  42. }
  43. if (h<0)
  44. {
  45. bdt=BulletDirection.LEFT;
  46. this.transform.rotation=Quaternion.Euler(new Vector3(0,0,0));
  47. rigidBody.velocity=new Vector2(h*moveSpeed,rigidBody.velocity.y);
  48. anim.PlayWalkAnimation();
  49. }
  50. #endregion
  51. #region 控制跳跃
  52. if (Input.GetKeyDown(KeyCode.K) && rigidBody.velocity.y==0)
  53. {
  54. rigidBody.AddForce(Vector2.up*upForce);
  55. anim.PlayJumpAnimation();
  56. }
  57. #endregion
  58. #region 控制射击
  59. if(Input.GetKey(KeyCode.J)&&Input.GetKey(KeyCode.W))
  60. {
  61. shoot.StartShoot(ShootDirection.UP);
  62. anim.PlayShootUpAnimation();
  63. }
  64. else if(Input.GetKey(KeyCode.J))
  65. {
  66. shoot.StartShoot(ShootDirection.FORWARD);
  67. anim.PlayShootForwardAnimation();
  68. }
  69. else
  70. {
  71. shoot.StopShoot();
  72. }
  73. #endregion
  74. #region 控制扔手榴弹
  75. if(Input.GetKey(KeyCode.L))
  76. {
  77. grenade.StartGrenadeAnim();
  78. anim.PlayTrowAnimation();
  79. }
  80. else
  81. {
  82. grenade.StopGrenadeAnim();
  83. }
  84. #endregion
  85. }
  86. #region 获取子弹设计方向(左右)
  87. public BulletDirection GetCurrentBulletDirection()
  88. {
  89. return bdt;
  90. }
  91. #endregion
  92. }


这样主角的动画,左右移动,跳跃移动,子弹射击,炸弹投放已经准备好了


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/119127
推荐阅读
相关标签
  

闽ICP备14008679号