当前位置:   article > 正文

unity中常见的角色控制方法_unity引擎角色控制代码

unity引擎角色控制代码

使用物理引擎(如 Rigidbody)来控制角色

  1. using UnityEngine;
  2. public class PlayerController : MonoBehaviour
  3. {
  4. public float moveSpeed = 5f;
  5. public float jumpForce = 5f;
  6. private Rigidbody rb;
  7. private bool isGrounded;
  8. void Start()
  9. {
  10. // 获取角色的 Rigidbody 组件
  11. rb = GetComponent<Rigidbody>();
  12. }
  13. void Update()
  14. {
  15. // 获取水平和垂直输入(键盘的方向键或 WASD)
  16. float moveHorizontal = Input.GetAxis("Horizontal");
  17. float moveVertical = Input.GetAxis("Vertical");
  18. // 创建一个新的 Vector3 来表示移动方向
  19. Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
  20. // 应用移动速度
  21. rb.velocity = movement * moveSpeed;
  22. // 检测跳跃输入
  23. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  24. {
  25. // 应用跳跃力
  26. rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
  27. }
  28. }
  29. void OnCollisionStay(Collision collision)
  30. {
  31. // 检测角色是否在地面上
  32. if (collision.gameObject.CompareTag("Ground"))
  33. {
  34. isGrounded = true;
  35. }
  36. }
  37. void OnCollisionExit(Collision collision)
  38. {
  39. // 检测角色是否离开地面
  40. if (collision.gameObject.CompareTag("Ground"))
  41. {
  42. isGrounded = false;
  43. }
  44. }
  45. }

使用角色控制器CharacterController 组件来控制角色

  1. using UnityEngine;
  2. public class PlayerController : MonoBehaviour
  3. {
  4. public float moveSpeed = 5f;
  5. public float jumpHeight = 1.5f;
  6. private CharacterController controller;
  7. private Vector3 playerVelocity;
  8. private bool groundedPlayer;
  9. void Start()
  10. {
  11. // 获取角色的 CharacterController 组件
  12. controller = GetComponent<CharacterController>();
  13. }
  14. void Update()
  15. {
  16. // 检测角色是否在地面上
  17. groundedPlayer = controller.isGrounded;
  18. if (groundedPlayer && playerVelocity.y < 0)
  19. {
  20. playerVelocity.y = 0f;
  21. }
  22. // 获取水平和垂直输入(键盘的方向键或 WASD)
  23. float moveHorizontal = Input.GetAxis("Horizontal");
  24. float moveVertical = Input.GetAxis("Vertical");
  25. // 创建一个新的 Vector3 来表示移动方向
  26. Vector3 move = new Vector3(moveHorizontal, 0, moveVertical);
  27. controller.Move(move * Time.deltaTime * moveSpeed);
  28. // 改变角色面朝移动方向
  29. if (move != Vector3.zero)
  30. {
  31. gameObject.transform.forward = move;
  32. }
  33. // 检测跳跃输入
  34. if (Input.GetButtonDown("Jump") && groundedPlayer)
  35. {
  36. playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * Physics.gravity.y);
  37. }
  38. // 应用重力
  39. playerVelocity.y += Physics.gravity.y * Time.deltaTime;
  40. controller.Move(playerVelocity * Time.deltaTime);
  41. }
  42. }

通过修改 Transform 组件的位置和旋转来控制角色

  1. using UnityEngine;
  2. public class PlayerController : MonoBehaviour
  3. {
  4. public float moveSpeed = 5f;
  5. public float rotationSpeed = 700f;
  6. public float jumpForce = 5f;
  7. private bool isGrounded;
  8. private Vector3 jump;
  9. void Start()
  10. {
  11. // 初始化跳跃向量
  12. jump = new Vector3(0.0f, jumpForce, 0.0f);
  13. }
  14. void Update()
  15. {
  16. // 获取水平和垂直输入(键盘的方向键或 WASD)
  17. float moveHorizontal = Input.GetAxis("Horizontal");
  18. float moveVertical = Input.GetAxis("Vertical");
  19. // 移动角色
  20. transform.Translate(new Vector3(moveHorizontal, 0, moveVertical) * moveSpeed * Time.deltaTime);
  21. // 旋转角色
  22. if (moveHorizontal != 0 || moveVertical != 0)
  23. {
  24. Vector3 direction = new Vector3(moveHorizontal, 0, moveVertical);
  25. Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
  26. transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
  27. }
  28. // 检测跳跃输入
  29. if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  30. {
  31. GetComponent<Rigidbody>().AddForce(jump, ForceMode.Impulse);
  32. }
  33. }
  34. void OnCollisionStay(Collision collision)
  35. {
  36. // 检测角色是否在地面上
  37. if (collision.gameObject.CompareTag("Ground"))
  38. {
  39. isGrounded = true;
  40. }
  41. }
  42. void OnCollisionExit(Collision collision)
  43. {
  44. // 检测角色是否离开地面
  45. if (collision.gameObject.CompareTag("Ground"))
  46. {
  47. isGrounded = false;
  48. }
  49. }
  50. }

设置Navigation导航,角色增加Nav Mesh Agent组件,使用SetDestination(目标位置)来进行角色移动

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

闽ICP备14008679号