马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
利用物理引擎(如 Rigidbody)来控制脚色
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed = 5f;
- public float jumpForce = 5f;
- private Rigidbody rb;
- private bool isGrounded;
- void Start()
- {
- // 获取角色的 Rigidbody 组件
- rb = GetComponent<Rigidbody>();
- }
- void Update()
- {
- // 获取水平和垂直输入(键盘的方向键或 WASD)
- float moveHorizontal = Input.GetAxis("Horizontal");
- float moveVertical = Input.GetAxis("Vertical");
- // 创建一个新的 Vector3 来表示移动方向
- Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
- // 应用移动速度
- rb.velocity = movement * moveSpeed;
- // 检测跳跃输入
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- {
- // 应用跳跃力
- rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
- }
- }
- void OnCollisionStay(Collision collision)
- {
- // 检测角色是否在地面上
- if (collision.gameObject.CompareTag("Ground"))
- {
- isGrounded = true;
- }
- }
- void OnCollisionExit(Collision collision)
- {
- // 检测角色是否离开地面
- if (collision.gameObject.CompareTag("Ground"))
- {
- isGrounded = false;
- }
- }
- }
复制代码 利用脚色控制器CharacterController 组件来控制脚色
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed = 5f;
- public float jumpHeight = 1.5f;
- private CharacterController controller;
- private Vector3 playerVelocity;
- private bool groundedPlayer;
- void Start()
- {
- // 获取角色的 CharacterController 组件
- controller = GetComponent<CharacterController>();
- }
- void Update()
- {
- // 检测角色是否在地面上
- groundedPlayer = controller.isGrounded;
- if (groundedPlayer && playerVelocity.y < 0)
- {
- playerVelocity.y = 0f;
- }
- // 获取水平和垂直输入(键盘的方向键或 WASD)
- float moveHorizontal = Input.GetAxis("Horizontal");
- float moveVertical = Input.GetAxis("Vertical");
- // 创建一个新的 Vector3 来表示移动方向
- Vector3 move = new Vector3(moveHorizontal, 0, moveVertical);
- controller.Move(move * Time.deltaTime * moveSpeed);
- // 改变角色面朝移动方向
- if (move != Vector3.zero)
- {
- gameObject.transform.forward = move;
- }
- // 检测跳跃输入
- if (Input.GetButtonDown("Jump") && groundedPlayer)
- {
- playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * Physics.gravity.y);
- }
- // 应用重力
- playerVelocity.y += Physics.gravity.y * Time.deltaTime;
- controller.Move(playerVelocity * Time.deltaTime);
- }
- }
复制代码 通过修改 Transform 组件的位置和旋转来控制脚色
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed = 5f;
- public float rotationSpeed = 700f;
- public float jumpForce = 5f;
- private bool isGrounded;
- private Vector3 jump;
- void Start()
- {
- // 初始化跳跃向量
- jump = new Vector3(0.0f, jumpForce, 0.0f);
- }
- void Update()
- {
- // 获取水平和垂直输入(键盘的方向键或 WASD)
- float moveHorizontal = Input.GetAxis("Horizontal");
- float moveVertical = Input.GetAxis("Vertical");
- // 移动角色
- transform.Translate(new Vector3(moveHorizontal, 0, moveVertical) * moveSpeed * Time.deltaTime);
- // 旋转角色
- if (moveHorizontal != 0 || moveVertical != 0)
- {
- Vector3 direction = new Vector3(moveHorizontal, 0, moveVertical);
- Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
- }
- // 检测跳跃输入
- if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
- {
- GetComponent<Rigidbody>().AddForce(jump, ForceMode.Impulse);
- }
- }
- void OnCollisionStay(Collision collision)
- {
- // 检测角色是否在地面上
- if (collision.gameObject.CompareTag("Ground"))
- {
- isGrounded = true;
- }
- }
- void OnCollisionExit(Collision collision)
- {
- // 检测角色是否离开地面
- if (collision.gameObject.CompareTag("Ground"))
- {
- isGrounded = false;
- }
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |