Unity 1인칭 컨트롤러(FirstPersonController)

1인칭 구조

여기에 이미지 설명을 삽입하세요.

카메라 배치

여기에 이미지 설명을 삽입하세요.

캐릭터컨트롤러

여기에 이미지 설명을 삽입하세요.

기본 아이디어:
주변의 카메라를 제어하세요x축 회전=> 문자위아래로뷰 회전,
몸체(실린더) 제어~에 대한회전 => 캐릭터를 제어하는 ​​스크립트~에 대한관점 회전.

여기에 이미지 설명을 삽입하세요.

중력 시뮬레이션

캐릭터 사용의 단점)
여기에 이미지 설명을 삽입하세요.

여기에 이미지 설명을 삽입하세요.

암호

MouseLook.cs

여기에 이미지 설명을 삽입하세요.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    
    
    //sensitivity => 灵敏度
    public float MouseSensitivity = 100f;

    public Transform PlayerBody;

    private float XRotation = 0f;


    // Start is called before the first frame update
    void Start()
    {
    
    
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    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);

        transform.localRotation = Quaternion.Euler(XRotation, 0f, 0f);

        PlayerBody.Rotate(Vector3.up * MouseX);

    }
}

PlayerMovement.cs

여기에 이미지 설명을 삽입하세요.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    
    
    public CharacterController CharacterController;

    [Header("Move")]
    public float Speed = 12f;
    public float Gravity = 20f;
    private Vector3 m_Velocity;
    [Space]
    [Header("IsGrounded")]
    public Transform GroundCheck;
    public float GroundDistance;
    public LayerMask GroundMask;

    private bool m_IsGrounded;

    [Header("Jump")]
    public float JumpHeight;

    // Update is called once per frame
    void Update()
    {
    
    
        // 落地检测
        m_IsGrounded = Physics.CheckSphere(GroundCheck.position, GroundDistance, GroundMask);
        if (m_IsGrounded && m_Velocity.y < 0)
        {
    
    
            m_Velocity.y = -2;
        }

        //平面移动
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");

        Vector3 MoveDir = transform.right * x + transform.forward * y;

        CharacterController.Move(MoveDir * Speed * Time.deltaTime);

        //自由落体
        m_Velocity.y -= Time.deltaTime * Gravity;

        CharacterController.Move(m_Velocity * Time.deltaTime);

        //跳跃
        if (Input.GetButtonDown("Jump")
            && m_IsGrounded)
        {
    
    
            m_Velocity.y = Mathf.Sqrt(JumpHeight * 2f * Gravity);
        }

    }
}

몇 가지 작은 확장

커서.잠금 상태

커서 잠금 모드
여기에 이미지 설명을 삽입하세요.

잠김: 커서를 잠급니다.스크린 센터,표시할 수 없음.
제한됨: 커서를 잠급니다.게임 인터페이스 내에서, 밖으로 이동할 수 없음, 커서표시됩니다

추천

출처blog.csdn.net/weixin_44293055/article/details/109523635