ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

20240112-【UNITY 学习】实现第一人称移动教程

2026/8/9 9:03:02 拓冰建站 浏览量
20240112-【UNITY 学习】实现第一人称移动教程

1、创建一个空物体,挂载Rigidbody组件,并设置相应参数

在这里插入图片描述

2、在上述空物体下创建一个胶囊体,两个空物体,一个用来控制朝向,另一个用来控制摄像机

在这里插入图片描述

3、给摄像机创建一个父物体,并挂载脚本MoveCamera_01.cs

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MoveCamera_01 : MonoBehaviour
{public Transform cameraPosition;private void Update(){transform.position = cameraPosition.position;}
}

4、给摄像机挂载脚本PlayerCam_01.cs

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerCam_01 : MonoBehaviour
{// 视觉灵敏度参数public float sensX = 400;public float sensY = 400;// 视角垂直旋转角度限制public float minAngle = -90f;public float maxAngle = 90f;// 角色朝向的 Transform,用于水平旋转public Transform orientation;// 当前的 X 和 Y 旋转角度private float xRotation;private float yRotation;private void Start(){// 初始时锁定鼠标光标并隐藏光标Cursor.lockState = CursorLockMode.Locked;Cursor.visible = false;}private void Update(){// 获取鼠标输入float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;// 更新水平旋转角度yRotation += mouseX;// 更新垂直旋转角度,并限制在指定范围内xRotation -= mouseY;xRotation = Mathf.Clamp(xRotation, minAngle, maxAngle);// 应用旋转到摄像机的 Transform 上,实现视角旋转transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);// 应用水平旋转到角色的 Transform 上,使角色朝向与摄像机一致orientation.rotation = Quaternion.Euler(0, yRotation, 0);}
}

5、给主角挂载脚本PlayerMovement_01.cs

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerMovement_01 : MonoBehaviour
{public float moveSpeed = 7; // 玩家移动速度public float groundDrag = 5; // 地面时的阻力public float playerHeight = 2; // 玩家身高public LayerMask whatIsGround; // 地面的LayerMaskprivate bool grounded; // 是否在地面上public float jumpForce = 6; // 跳跃力度public float jumpCooldown = 0.25f; // 跳跃冷却时间public float airMultiplier = 0.4f; // 空中移动速度衰减private bool readyToJump = true; // 是否可以跳跃public KeyCode jumpKey = KeyCode.Space; // 跳跃键public Transform orientation; // 玩家朝向的Transformprivate float h; // 水平输入private float v; // 垂直输入private Vector3 moveDirection; // 移动方向private Rigidbody rb; // 玩家刚体private void Start(){rb = GetComponent<Rigidbody>();rb.freezeRotation = true; // 防止刚体旋转}private void Update(){grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);MyInput();SpeedControl();if (grounded)rb.drag = groundDrag;elserb.drag = 0;}private void FixedUpdate(){MovePlayer();}private void MyInput(){h = Input.GetAxisRaw("Horizontal");v = Input.GetAxisRaw("Vertical");if (Input.GetKey(jumpKey) && readyToJump && grounded){readyToJump = false;Jump();Invoke(nameof(ResetJump), jumpCooldown);}}private void MovePlayer(){// 根据朝向计算移动方向moveDirection = orientation.forward * v + orientation.right * h;if (grounded){rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);}else if (!grounded){rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);}}private void SpeedControl(){Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);if (flatVel.magnitude > moveSpeed){// 限制速度在设定范围内Vector3 limitedVel = flatVel.normalized * moveSpeed;rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);}}private void Jump(){//rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);rb.velocity = Vector3.zero;// 添加向上的力以实现跳跃rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);}private void ResetJump(){readyToJump = true;}
}