unity中常见的脚色控制方法

打印 上一主题 下一主题

主题 535|帖子 535|积分 1605

利用物理引擎(如 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. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表