2023/12/25

์•ˆ์„ํ™˜ยท2023๋…„ 12์›” 25์ผ
0

์˜ค๋Š˜ ๋ฐฐ์šด ์ฝ”๋“œ


1. ์ปค์„œ๋ฅผ ๋ณด์ด์ง€ ์•Š๊ฒŒ ์ž ๊ทธ๊ธฐ

๐Ÿ’พ ์ฝ”๋“œ


using UnityEngine;

public class PlayerController : MonoBehaviour
{
	void Start //์‹œ์ž‘ํ•  ๋•Œ
	{
		Cursor.lockState = CursorLockMode.Locked; //์ปค์„œ๋ฅผ ์ค‘์•™์— ๊ณ ์ •
        
        //์‹ฌํ™”: ์ปค์„œ์˜ ์ปค์„œ๋ฝ ๋ชจ๋“œ์˜ ๋ฝ์Šคํ…Œ์ดํŠธ ์„ค์ •์„ (=) ์ปค์„œ๋ฝ ๋ชจ๋“œ(enum)์˜ ๊ฐ’์ค‘ ์ปค์„œ๋ฅผ ํ™”๋ฉด ์ค‘์•™์— ๊ณ ์ •(Locked)์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค
	}
}

๐Ÿ“– ์ฐธ๊ณ 

namespace UnityEngine;

[NativeHeader("Runtime/Export/Input/Cursor.bindings.h")]

public class Cursor
{
    public static extern CursorLockMode lockState
    {
        [MethodImpl(MethodImplOptions.InternalCall)]
        get;
        [MethodImpl(MethodImplOptions.InternalCall)]
        set;
    }
}

public enum CursorLockMode
{

    //์ปค์„œ์˜ ํ–‰๋™์„ ํŠน์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค
    None,
    
    //์ปค์„œ๋ฅผ ํ™”๋ฉด์˜ ์ •์ค‘์•™์— ๊ณ ์ •ํ•œ๋‹ค
    Locked,
    
    //์ปค์„œ๋ฅผ ๊ฒŒ์ž„ ์ฐฝ์•ˆ์— ๊ฐ€๋‘”๋‹ค
    Confined
}

  1. ๋งˆ์šฐ์Šค์˜ delta ๊ฐ’์„ ๋ฐ›์•„ ์นด๋ฉ”๋ผ ํšŒ์ „ ์‹œํ‚ค๊ธฐ

๐Ÿ’พ ์ฝ”๋“œ


using UnityEngine;

public class PlayerController : MonoBehaviour
{
	void CameraLook() //์นด๋ฉ”๋ผ์˜ ์‹œ์•ผ ๋ฉ”์†Œ๋“œ
    {
        camCurXRot += mouseDelta.y * lookSensitivity; 
        //์นด๋ฉ”๋ผ์˜ X Rotate ๊ฐ’์— ๋”ํ•ด์ค€๋‹ค(+=) ๋งˆ์šฐ์Šค์˜ Y ์ขŒํ‘œ๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค ์ด๋•Œ ๊ฐ๋„ ์„ค์ •์„ ๊ณฑํ•ด์ฃผ์–ด ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค
        //๋งˆ์šฐ์Šค์˜ Y๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด X์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ๋Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— X์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค
        
        camCurXRot = Mathf.Clamp(camCurXRot,minXLook,maxXLook);
        //์นด๋ฉ”๋ผ์˜ X Rotate ๊ฐ’์„ ์ œํ•œํ•œ๋‹ค
        //Clamp(float value: ๊ด€๋ฆฌํ•  float๊ฐ’, ์ตœ์†Ÿ๊ฐ’, ์ตœ๋Œ“๊ฐ’)
        
        cameraContainer.localEulerAngles = new Vector3(-camCurXRot, 0, 0);
        //์นด๋ฉ”๋ผ ์ปจํ…Œ์ด๋„ˆ(์นด๋ฉ”๋ผ๋ฅผ ๋‹ด๊ณ ์žˆ๋Š” obj)๋ฅผ ์˜ค์ผ๋Ÿฌ์•ต๊ธ€(0 ~ 3.14๊ธฐ์ค€์ด ์•„๋‹Œ 0 ~ 360๊ธฐ์ค€)๊ธฐ์ค€ ๊ฐ๋„๋ฅผ(=) ์ƒˆ๋กœ์šด 3์ฐจ์›(๊ณ„์‚ฐํ•œ ๋งˆ์šฐ์Šค๊ฐ€ Y๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ธ ๊ฐ’์„ ๋นผ์ค€๋‹ค, 0, 0)๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค
        //์˜ค๋ธŒ์ ํŠธ๊ฐ€ X์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ๋”ํ•ด์ง€๋ฉด ์‹œ๊ณ„๋ฐฉํ–ฅ ํšŒ์ „์„ ํ•˜๊ธฐ๋–„๋ฌธ์— ๋นผ์ฃผ์–ด Y์ขŒํ‘œ๊ฐ’์„ ๋นผ์ฃผ์–ด ๋ฐ˜์‹œ๊ณ„ ํšŒ์ „ํ•˜๊ฒŒ ๋ฐ”๊ฟ”์ค€๋‹ค
        //์˜ค๋ธŒ์ ํŠธ์˜ ์œ„ ์•„๋ž˜ ํšŒ์ „์ด ์•„๋‹Œ ์นด๋ฉ”๋ผ์˜ ํšŒ์ „ ๊ฐ’์„ ์กฐ์ •ํ•˜์—ฌ ์–ผ๊ตด์˜ ์œ„ ์•„๋ž˜ ์›€์ง์ž„์„ ๊ตฌํ˜„ํ–ˆ๋‹ค
        //local์ด ๋ถ™๋Š” ์ด์œ ๋Š” ์ƒ์œ„ ๊ฐ์ฒด์˜ ํšŒ์ „์— ์˜ํ–ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค

        transform.eulerAngles += new Vector3(0, mouseDelta.x * lookSensitivity, 0);
        //์˜ค๋ธŒ์ ํŠธ์˜ ์ขŒ์šฐ ํšŒ์ „๊ฐ’์„ ์กฐ์ •ํ•ด์ค€๋‹ค
        //์˜ค๋ธŒ์ ํŠธ์˜ ์–ผ๊ตด์€ ์œ„์•„๋ž˜๋กœ๋งŒ ์›€์ง์ด๊ณ  ์ขŒ์šฐ๋Š” ๊ณ ์ •ํ•˜์—ฌ ์˜†์„ ๋ณผ๋ ค๋ฉด ๋ชธํ†ต์ด ํšŒ์ „ํ•˜๊ฒŒ ๋˜๋Š” ์›๋ฆฌ์ด๋‹ค
        
    }
}

  1. Input System์˜ ๋งˆ์šฐ์Šค ์ž…๋ ฅ๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ

๐Ÿ’พ ์ฝ”๋“œ


using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public void OnLookInput(InputAction.CallbackContext context)
    //๋งค๊ฐœ๋ณ€์ˆ˜๋Š” Input System์˜ ๋งˆ์šฐ์Šค์˜ ๋ธํƒ€๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” Look ๊ฐ’์„ ๋ฐ›์•„์˜ฌ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
    {
        mouseDelta = context.ReadValue<Vector2>();
        //๋งˆ์šฐ์Šค๋ธํƒ€ ๋ณ€์ˆ˜๋Š”(=) ๋ฐ›์•„์˜จ Input System์˜ x, y ๊ฐ’๋งŒ ๋ฐ›์•„์˜จ๋‹ค.
    }
}

  1. Input System์˜ ๋งˆ์šฐ์Šค ์ž…๋ ฅ๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ

๐Ÿ’พ ์ฝ”๋“œ


using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public void OnMoveInput(InputAction.CallbackContext context)
    {
        if(context.phase == InputActionPhase.Performed)
        //Input System์˜ ํ˜„์žฌ(phase)์ƒํƒœ์˜ ์—ด๊ฑฐํ˜•(InputActionPhase)๊ฐ’์ด(=) ์ž‘๋™์ค‘(Performed)์ด๋ผ๋ฉด
        
        {
            curMovementInput = context.ReadValue<Vector2>();
            //์ด๋™๊ฐ’์„(=) Input System(context)์ค‘ 2์ฐจ์›๊ฐ’์„ ๋ฐ›์•„์˜จ๋‹ค
        }
        else if(context.phase == InputActionPhase.Canceled)
        //์ทจ์†Œ๋˜์—ˆ๋‹ค๋ฉด
        {
            curMovementInput = Vector2.zero;
            //์ด๋™๊ฐ’์„(=) 0,0 ์œผ๋กœ ๋ฐ”๊พผ๋‹ค
        }
    }
}

๐Ÿ“– ์ฐธ๊ณ 

namespace UnityEngine.InputSystem;

public enum InputActionPhase
{
    Disabled,
    Waiting,
    Started,
    Performed,
    Canceled
}




โ›”๏ธ ์—๋Ÿฌ


  1. ๋‚˜๋Š” Visual Studio ๋ฅผ ์‚ฌ์šฉ์ค‘์ธ๋ฐ ๋งฅ๋ถ์€ Visual Studio Code๋ฅผ ๋งŽ์€๋ถ„์ด ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ ๊ฐ™์•˜๋‹ค
  • Visaul Studio Code๋ฅผ ์„ค์น˜ํ•˜๊ณ  C# ๊ฐœ๋ฐœ ์„ธํŒ…์„ ํ•ด์ฃผ์—ˆ๋‹ค
  1. Visual Studio Code๊ฐ€ ์•„์ง IntelliCode์˜ ๊ธฐ๋Šฅ์ค‘ ์˜ˆ์ƒ์ฝ”๋“œ๋ฅผ ํšŒ์ƒ‰ ํƒ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์•ˆ๋˜๊ณ  ์žˆ์—ˆ๋‹ค
  • .Net ์„ 8.0(์ตœ์‹ )์—์„œ 6.0์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์–ด๋ณด์•˜๋‹ค(์‹คํŒจ)
  • VS Code ์˜ ์„ค์ •์„ ์ด๋ž˜์ €๋ž˜ ๋งŒ์ ธ๋ณด์•˜๋‹ค(์‹คํŒจ)
  1. Velog ์“ฐ๋Š” ํ˜•์‹์„ ์ข€ ๋” ๊ตฌ์ฒด์ ์ด๊ณ  ์ฒด๊ณ„์ ์ด๊ณ  ๋ณด๊ธฐ ์ข‹๊ฒŒ ๋ฐ”๊ฟ€ ํ•„์š”๊ฐ€ ์žˆ์–ด ๋ณด์˜€๋‹ค
  • Velog ๊ธฐ๋Šฅ์„ ๊ณต๋ถ€ํ•˜๊ณ  ์ ์šฉ์„ ํ•˜๋‚˜์”ฉ ํ•ด๋ณด๊ณ ์žˆ๋‹ค




๐Ÿ’ญ ๋Š๋‚€์ 


  1. ๋ฌธ์ œ ํ•ด๊ฒฐ์— ์ œํ•œ์‹œ๊ฐ„ ์„ค์ •์ด ํ•„์š”ํ•ด ๋ณด์ธ๋‹ค
  • ์˜ˆ์ƒ์ฝ”๋“œ ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„์„ ์Ÿ์•˜๋‹ค
  1. ํ• ์ผ์˜ ์ˆœ์„œ๋ฅผ ๋ช…ํ™•ํžˆ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค
  • ๊ฐ•์˜ ์™„๊ฐ•์ด ๋จผ์ „๋ฐ ๋ˆˆ์•ž์˜ ๋ฌธ์ œ์— ๋„ˆ๋ฌด ์‹œ๊ฐ„์„ ์Ÿ์•„๋ฒ„๋ ธ๋‹ค
  1. ๊ณต๋ถ€ ์™ธ์—๋„ ์ข€๋” ์ฒด๊ณ„์ ์ธ ์‚ถ์„ ์‚ด ํ•„์š”๊ฐ€ ์žˆ๋‹ค
  • ๊ณต๋ถ€๋ฅผ ์œ„ํ•ด์„œ๋ผ๋„ ๊ณต๋ถ€ ์™ธ์˜ ํ•  ๊ฒƒ(์ง‘์•ˆ์ผ, ์šด๋™, ๋…์„œ๋“ฑ)์„ ๋งค์ผ ์ ์ ˆํ•œ ๋ฐฐ๋ถ„์„ ํ•  ํ•„์š”๋ฅผ ๋Š๊ผ‡๋‹ค
  1. ๊ฒŒ์ž„์ ‘์ž
  • ์ ์–ด๋„ ๊ฐ•์˜ ๋‹ค ๋“ค์„ ๋•Œ ๊นŒ์ง„ ์ ‘๋Š”๋‹ค
profile
์•ˆ์„ํ™˜!

0๊ฐœ์˜ ๋Œ“๊ธ€