유니티 Charactercontroller 컴포넌트

cometrue·2024년 8월 12일
0

CharacterController는 3인칭 또는 1인칭 플레이어 캐릭터의 움직임을 제어하는데 사용되는 컴포넌트이다.
CharacterController는 Rigidbody와 달리 물리적 충돌을 고려하지 않으며, 플레이어의 입력을 사용하여 캐릭터의 움직임을 제어한다
ChracterController의 주요 기능은 다음과 같다.\

  • 플레이어의 입력을 사용하여 캐릭터의 움직임을 제어한다.

  • 플레이어가 장애물과 충돌할 때 충돌 처리를 수행한다.

  • 플레이어가 계단이나 경사로를 올라갈 때의 움직을 제어한다.


플레이어의 정면을 구분할 수 있게 만든다.
기본지형을 만든다.

Player 오브젝트에게 Player Input컴포넌트를 추가한 후에 Input.cs에서 따로 입력을 받아오고
Player Controller에서 움직임을 구현 할 것이다.


Player Inputd을 사용하기 위해서는 InputSystem 추가가 필요하다

using UnityEngine;
using UnityEngine.InputSystem;

public class Input : MonoBehaviour
{
    public Vector2 move;


    public void OnMove(InputValue value)
    {
        MoveInput(value.Get<Vector2>());
    }

    public void MoveInput(Vector2 newMoveDirection)
    {
        move = newMoveDirection;
    }

    private void Update()
    {
        Debug.Log(move);
    }
}

그리고 PlayerController에서 움직임을 구현한다.

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

public class PlayerController : MonoBehaviour
{
    private Input _input;
    private CharacterController _controller;

    public float speed;

    void Start()
    {
        _input = GetComponent<Input>();
        _controller = GetComponent<CharacterController>();
    }


    private void Update()
    {
        Move();
    }


    private void Move()
    {      
        Vector3 inputDirection = new Vector3(_input.move.x,0.0f,_input.move.y).normalized;

        // Player를 움직인다. 
        _controller.Move(inputDirection.normalized * speed * Time.deltaTime);
                 
    }

}

아주 기본적으로 다음과 같이 움직일 수 있다.

profile
게임개발자

0개의 댓글