wasd移动 鼠标旋转方向 空格跳跃 ctrl蹲
先新建空物体player 绑定 characterController和 FirstPersonController组件
然后相机为子物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour
{
public float speed = 5.0f; // 移动速度
public float mouseSensitivity = 2.0f; // 鼠标灵敏度
public float jumpSpeed = 8.0f; // 跳跃速度
public float gravity = 20.0f; // 重力
private CharacterController characterController;
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
private Camera playerCamera;
public float crouchHeight = 0.5f; // 蹲下高度
public float normalHeight = 2.0f; // 正常高度
public float crouchSpeed = 2.5f; // 蹲下时的移动速度
private bool isCrouching = false;
void Start()
{
characterController = GetComponent<