Unity第一人称漫游简单好用(和cf一样)

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<CharacterController>();
        playerCamera = Camera.main;

        // 隐藏并锁定光标
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        // 视角旋转
        rotationX += Input.GetAxis("Mouse X") * mouseSensitivity;
        rotationY -= Input.GetAxis("Mouse Y") * mouseSensitivity;
        rotationY = Mathf.Clamp(rotationY, -90, 90);

        playerCamera.transform.localRotation = Quaternion.Euler(rotationY, 0, 0);
        transform.rotation = Quaternion.Euler(0, rotationX, 0);

        // 移动
        if (characterController.isGrounded)
        {
            float moveDirectionY = moveDirection.y;
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= isCrouching ? crouchSpeed : speed; // 如果在蹲下状态则使用蹲下的移动速度
            moveDirection.y = moveDirectionY;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
        // 蹲下功能
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Crouch();
        }
        if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            StandUp();
        }
        // 应用重力
        moveDirection.y -= gravity * Time.deltaTime;
        characterController.Move(moveDirection * Time.deltaTime);
    }

    void OnApplicationFocus(bool hasFocus)
    {
        // 当应用获得焦点时锁定光标
        if (hasFocus)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }
    void Crouch()
    {
        characterController.height = crouchHeight;
        isCrouching = true;
    }

    void StandUp()
    {
        characterController.height = normalHeight;
        isCrouching = false;
    }
}

 然后启动游戏就好了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值