当前位置:   article > 正文

Unity移动,跳跃(这篇使用Rigidbody和CharacterController两种方式)_unity rightbody跳跃

unity rightbody跳跃

1,Rigidbody

  1. using UnityEngine;
  2. public class Move : MonoBehaviour
  3. {
  4. /// <summary>
  5. /// 移动速度
  6. /// </summary>
  7. public float moveSpeed = 1f;
  8. /// <summary>
  9. /// 转弯速度
  10. /// </summary>
  11. public float rotateSpeed = 60f;
  12. /// <summary>
  13. /// 跳跃力
  14. /// </summary>
  15. public float jumpVelocity = 5.0f;
  16. private float vInput;
  17. private float hInput;
  18. private float JInput;
  19. private Rigidbody _rb;
  20. /// <summary>
  21. /// 判断触碰地面
  22. /// </summary>
  23. private bool _isGrounded;
  24. void Start()
  25. {
  26. _rb = GetComponent<Rigidbody>();
  27. }
  28. void Update()
  29. {
  30. vInput = Input.GetAxis("Vertical") * moveSpeed;
  31. hInput = Input.GetAxis("Horizontal") * moveSpeed;
  32. if (Input.GetKeyDown(KeyCode.Space))
  33. {
  34. //判断是否在地面
  35. if (!_isGrounded)
  36. {
  37. JInput = jumpVelocity;
  38. }
  39. }
  40. }
  41. void FixedUpdate()
  42. {
  43. //跳跃 添加一个向上的瞬间力ForceMode.Impulse,要跳的越高jumpVelocity值就越大
  44. _rb.AddForce(Vector3.up * JInput, ForceMode.Impulse);
  45. //执行完Update的 JInput = jumpVelocity,并且上行逻辑执行完毕后 执行 JInput = 0f,物体下落
  46. JInput = 0f;
  47. //移动
  48. if (Mathf.Abs(vInput) != 0 || Mathf.Abs(hInput) != 0)
  49. {
  50. _rb.MovePosition(this.transform.position + new Vector3(hInput * Time.fixedDeltaTime, 0, vInput * Time.fixedDeltaTime));
  51. }
  52. //旋转
  53. Vector3 rotation_V = new Vector3(vInput, 0, transform.position.x) * rotateSpeed;
  54. Vector3 rotation_H = new Vector3(transform.position.z, 0, hInput) * rotateSpeed;
  55. Quaternion angleRot_V = Quaternion.Euler(rotation_V * Time.fixedDeltaTime);
  56. Quaternion angleRot_H = Quaternion.Euler(rotation_H * Time.fixedDeltaTime);
  57. if (Mathf.Abs(vInput) != 0)
  58. {
  59. _rb.MoveRotation(_rb.rotation * angleRot_V);
  60. }
  61. if (Mathf.Abs(hInput) != 0)
  62. {
  63. _rb.MoveRotation(_rb.rotation * angleRot_H);
  64. }
  65. }
  66. /// <summary>
  67. /// 碰撞检测刚触碰到时_isGrounded = false;可以跳跃
  68. /// </summary>
  69. /// <param name="collision"></param>
  70. private void OnCollisionEnter(Collision collision)
  71. {
  72. if (collision.gameObject.tag.Equals("Grounded"))
  73. {
  74. _isGrounded = false;
  75. }
  76. }
  77. /// <summary>
  78. /// 碰撞检测触碰结束时_isGrounded = true;不可以跳跃
  79. /// </summary>
  80. /// <param name="collision"></param>
  81. private void OnCollisionExit(Collision collision)
  82. {
  83. if (collision.gameObject.tag.Equals("Grounded"))
  84. {
  85. _isGrounded = true;
  86. }
  87. }
  88. }

2,CharacterController

  1. using UnityEngine;
  2. public class Move : MonoBehaviour
  3. {
  4. private CharacterController controller;
  5. private Vector3 Direction;
  6. private Vector3 JumpDown;
  7. private float vInput;
  8. private float hInput;
  9. /// <summary>
  10. /// 移动速度
  11. /// </summary>
  12. private float moveSpeed = 1.5f;
  13. /// <summary>
  14. /// 跳跃力度
  15. /// </summary>
  16. private float jumpSpeed = 5f;
  17. /// <summary>
  18. /// 转弯速度
  19. /// </summary>
  20. private float rotateSpeed = 180f;
  21. /// <summary>
  22. /// 跳跃高度
  23. /// </summary>
  24. private float jumpHeight = 7f;
  25. /// <summary>
  26. /// 重力
  27. /// </summary>
  28. private float Gravity = 18f;
  29. void Start()
  30. {
  31. controller = GetComponent<CharacterController>();
  32. Direction = Vector3.zero;
  33. }
  34. void Update()
  35. {
  36. //判断是否在地面
  37. if (controller.isGrounded)
  38. {
  39. vInput = Input.GetAxis("Vertical");
  40. hInput = Input.GetAxis("Horizontal");
  41. Direction = new Vector3(hInput, 0f, vInput);
  42. Direction *= moveSpeed;
  43. //旋转
  44. Vector3 rotation_V = Vector3.right * vInput * rotateSpeed * Time.deltaTime;
  45. Vector3 rotation_H = Vector3.back * hInput * rotateSpeed * Time.deltaTime;
  46. if (Mathf.Abs(vInput) != 0)
  47. {
  48. transform.Rotate(rotation_V, Space.World);
  49. }
  50. if (Mathf.Abs(hInput) != 0)
  51. {
  52. transform.Rotate(rotation_H, Space.World);
  53. }
  54. if (Input.GetKeyDown(KeyCode.Space))
  55. {
  56. //跳跃高度
  57. JumpDown.y = jumpHeight;
  58. }
  59. }
  60. else
  61. {
  62. //不在地面随时间施加重力
  63. JumpDown.y -= Gravity * Time.deltaTime;
  64. }
  65. //移动加跳跃
  66. controller.Move((Direction + JumpDown) * jumpSpeed * Time.deltaTime);
  67. }
  68. }

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

闽ICP备14008679号