纳金网

标题: 人物控制脚本的实现 [打印本页]

作者: 狂风大尉    时间: 2014-6-26 17:07
标题: 人物控制脚本的实现

只是简单地实现了一下几个动作的调用,目前还在实验阶段,也不知道这样的调用方法可以一直延续下去不,当控制状态变得越来越多的时候,怎么样做到不互相影响,并且维护起来也是个问题,希望大家能一起考虑一下。也许可以借鉴一下已有项目的经验。

对D3D的空间几何知识是必要的,而这里要注意的是U3D提供了一个方法来判断是否与地面相碰撞,_grounded = (_flags & CollisionFlags.CollidedBelow) != 0;
其中CollisionFlags.CollidedBelow要想获得一个值,玩家的移动方向必须有朝向地面的方向向量分量。_grounded = (_flags & CollisionFlags.CollidedBelow) != 0;


为了在跳跃过程中更具有真实性,加入了重力加速度的概念,注意_gravity表示的是“重力速度”,也就是向下的一个向量分量。
  1. @script RequireComponent(CharacterController)
  2. var _speed : float;
  3. var _jumpspeed : float;
  4. var _rotatespeed : float;
  5. var _originalgravity : float;
  6. var _gravity : float;
  7. var _addgravity : float;
  8. var _moveDirection: Vector3;
  9. var _lastDirection: Vector3;
  10. var _originalRotation : Quaternion;
  11. var _flags;
  12. var _grounded;
  13. var _canjump;
  14. var _animation;
  15. var _run : AnimationClip;
  16. var _idle : AnimationClip;
  17. var _attack : AnimationClip;
  18. var _jump : AnimationClip;
  19. private var _controller : CharacterController;
  20. enum PlayerState
  21. {
  22. STATE_READY   = 0,
  23. STATE_RUN    = 1,
  24. STATE_IDLE    = 2,
  25. STATE_ATTACK   = 3,  
  26. STATE_JUMP    = 4,
  27. }
  28. private var _playerstate : PlayerState;
  29. function Start(){
  30. _speed = 50F;
  31. _jumpspeed = 40F;
  32. _rotatespeed = 10F;

  33. _originalgravity = 40;
  34. _gravity = 40;
  35. _addgravity = 3;

  36. _canjump = true;

  37. _moveDirection = Vector3.zero;
  38. _lastDirection = Vector3.zero;
  39. _originalRotation = new Quaternion(0f, 0f, 0f, 1f);

  40. _grounded = true;
  41. _animation = GetComponent(Animation);

  42. _controller = GetComponent(CharacterController);

  43. _playerstate = PlayerState.STATE_READY;

  44. _moveDirection.y = -1; //给Y轴方向一个初始值,以使其可以与地面正确相撞
  45. }
  46. function Update () {
  47. //模型的旋转(临时)
  48. if(Input.GetKey("a")){
  49.   transform.Rotate(-Vector3.up*2);
  50. }
  51. if(Input.GetKey("d")){
  52.   transform.Rotate(Vector3.up*2);
  53. }


  54. if(_grounded)
  55. {
  56.   //本地空间水平移动方向向量
  57.   _moveDirection = Vector3( 0 , -1 ,Input.GetAxis("Vertical"));
  58.   
  59.   //本地空间移动方向转为世界空间的移动方向向量
  60.   _moveDirection = transform.TransformDirection(_moveDirection);
  61.   _moveDirection *= _speed;
  62.   
  63.   //重力速度还原
  64.   _gravity = _originalgravity;
  65. }

  66. //跳跃
  67. if (Input.GetButton("Jump") )
  68. {
  69.   _playerstate = PlayerState.STATE_JUMP;
  70.   
  71.   //获得初速度
  72.   _moveDirection.y = _jumpspeed;
  73.   
  74.   _animation.Play("jump");
  75. }


  76. //如果当前处于跳跃中,就算一下Y向量
  77. if(_playerstate == PlayerState.STATE_JUMP)
  78. {
  79.   _gravity += _addgravity;
  80.   _moveDirection.y -= _gravity * Time.deltaTime;  
  81. }
  82.   
  83. //移动,并获得碰撞标示
  84. var _flags : CollisionFlags =_controller.Move(_moveDirection *Time.deltaTime);
  85. //是否已经着地
  86. _grounded = (_flags & CollisionFlags.CollidedBelow) != 0;
  87. print(_grounded);
  88. //=======================================================================
  89. //攻击
  90. if(Input.GetKeyUp("f") && _grounded &&_playerstate == PlayerState.STATE_IDLE)
  91. {
  92.   _playerstate = PlayerState.STATE_ATTACK;
  93.   _animation.Play("attack");
  94. }

  95. //休闲
  96. if(_moveDirection.x == 0  &&  _moveDirection.z == 0&& _grounded && _playerstate != PlayerState.STATE_ATTACK)
  97. if(_moveDirection != Vector3(0,0,0))
  98. {
  99.   if(_playerstate != PlayerState.STATE_ATTACK)
  100.   {
  101.    _playerstate = PlayerState.STATE_IDLE;
  102.    _animation.Play("idle");
  103.   }
  104. }

  105. //跑动
  106. if((_moveDirection.x != 0 || _moveDirection.z != 0) &&_grounded  )
  107. {
  108.    _playerstate = PlayerState.STATE_RUN;
  109.    _animation.Play("run");
  110. }
  111. }
  112. //供外部UI调用的接口,调用攻击动作
  113. function PlayAnimation_Attack()
  114. {
  115. if(_grounded && (_playerstate ==PlayerState.STATE_IDLE||_playerstate ==PlayerState.STATE_ATTACK))
  116. {
  117.   _playerstate = PlayerState.STATE_ATTACK;
  118.   _animation.Play("attack");
  119. }
  120. }
复制代码

作者: HIDEOKOJIMA    时间: 2014-6-26 17:14
Thanks for this !
作者: hyui    时间: 2014-6-26 18:40
Good to know !
作者: wucnj    时间: 2014-6-27 14:39
感谢分享!!!




欢迎光临 纳金网 (http://old.narkii.com/club/) Powered by Discuz! X2.5