Unity提供了多种实现人物移动的方法,每种方法适用于不同的场景和需求。以下是主要的实现方式:
1. 基础Transform移动
// 简单移动
void Update() {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
// 带输入的移动
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
transform.Translate(movement);
}
2. 角色控制器(CharacterController)
public CharacterController controller;
public float speed = 6f;
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 move = transform.right * horizontal + transform.forward * vertical;
controller.Move(move * speed * Time.deltaTime);
// 重力处理
if(!controller.isGrounded) {
controller.Move(Vector3.down * gravity * Time.deltaTime);
}
}
3. 物理系统(Rigidbody)移动
直接力应用
public Rigidbody rb;
public float forceAmount = 10f;
void FixedUpdate() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 force = new Vector3(horizontal, 0, vertical) * forceAmount;
rb.AddForce(force);
}
速度控制
public Rigidbody rb;
public float speed = 5f;
void FixedUpdate() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0, vertical) * speed;
rb.velocity = movement;
}
移动位置(MovePosition)
public Rigidbody rb;
public float speed = 5f;
void FixedUpdate() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0, vertical) * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
}
4. 导航系统(NavMesh)
基本导航
public NavMeshAgent agent;
public Transform target;
void Start() {
agent.SetDestination(target.position);
}
// 动态更新目标
void Update() {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
agent.SetDestination(hit.point);
}
}
}
自定义导航移动
public NavMeshAgent agent;
public float speed = 5f;
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
agent.Move(movement);
}
5. 动画系统驱动移动
根运动(Root Motion)
public Animator animator;
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
animator.SetFloat("Horizontal", horizontal);
animator.SetFloat("Vertical", vertical);
// 确保Animator组件上勾选了Apply Root Motion
}
6. 混合方法
角色控制器+物理
public CharacterController controller;
public float speed = 6f;
public float jumpForce = 8f;
private Vector3 playerVelocity;
private bool groundedPlayer;
void Update() {
groundedPlayer = controller.isGrounded;
if(groundedPlayer && playerVelocity.y < 0) {
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * speed);
if(move != Vector3.zero) {
transform.forward = move;
}
// 跳跃
if(Input.GetButtonDown("Jump") && groundedPlayer) {
playerVelocity.y += Mathf.Sqrt(jumpForce * -3.0f * Physics.gravity.y);
}
playerVelocity.y += Physics.gravity.y * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
7. 高级移动技术
第三人称摄像机跟随移动
public Transform cameraTransform;
public float speed = 5f;
public float turnSmoothTime = 0.1f;
private float turnSmoothVelocity;
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;
if(direction.magnitude >= 0.1f) {
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
第一人称移动
public float speed = 5f;
public float mouseSensitivity = 100f;
private float xRotation = 0f;
void Update() {
// 鼠标控制视角
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
Camera.main.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
// 键盘控制移动
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = transform.right * horizontal + transform.forward * vertical;
controller.Move(movement * speed * Time.deltaTime);
}
选择建议
简单原型/2D游戏:Transform移动或Rigidbody
第一人称/第三人称角色:CharacterController
物理模拟游戏:Rigidbody
AI导航:NavMeshAgent
动画驱动角色:Animator Root Motion
需要精确碰撞检测:CharacterController或Rigidbody
每种方法都有其优缺点,选择时应考虑游戏类型、性能需求、物理精度要求和开发复杂度等因素