使用物理引擎(如 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;}}
}