๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

 

์ €๋ฒˆ ๊ธ€์—์„œ ๊ธฐ๋ณธ์ ์ธ ์…‹ํŒ…์„ ๋๋ƒˆ์—ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์ž…๋ ฅ๊ฐ’์„ ์Šคํฌ๋ฆฝํŠธ(script)์— ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹ ์ค‘ 2๊ฐ€์ง€ ์ •๋„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ์ง์„ ์ œ์–ดํ•˜๋Š” ๊ฑธ ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. Player ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— Player Input ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถ€์ฐฉํ–ˆ์—ˆ๋‹ค. ํ‚ค๋ฅผ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด, ํ‚ค๋ณด๋“œ๋กœ ๋“ค์–ด์˜จ ์ž…๋ ฅ๊ฐ’์„ ์ฃผ์ธ๊ณต ์บ๋ฆญํ„ฐ์—๊ฒŒ ์ „๋‹ฌํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

 

 

1. ๋กœ์ง์„ ์ œ์–ดํ•  ์Šคํฌ๋ฆฝํŠธ(script) ๋ถ€์ฐฉํ•˜๊ธฐ

 

Player ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์— ํ‚ค ์ž…๋ ฅ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›๊ณ , ํ•ด๋‹น ๋กœ์ง์„ ์ œ์–ดํ•  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ถ™์—ฌ์คฌ๋‹ค.

 

์ž…๋ ฅ์ด ๋“ค์–ด์™”์„ ๋•Œ, ํ•ด๋‹น ๋‚ด์šฉ์„ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” 'Behavior'

 

  • Send Messages, Broadcast Messages  :  ์œ ๋‹ˆํ‹ฐ์˜ Send Message ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹
  • Invoke Unity Events, Invoke C Sharp Events  :  ์œ ๋‹ˆํ‹ฐ๋‚˜ C#์˜ ์ด๋ฒคํŠธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
  • ์œ„ ๋ฐฉ์‹๋“ค์„ ์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ ์šฉ์ด ํ•„์š”
using UnityEngine.InputSystem;

 

 

Send Messages ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

 

  • ํŠน์ • ํ‚ค๊ฐ€ ๋“ค์–ด์˜ค๋ฉด, ํŠน์ •ํ•œ ํ•จ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹
  • Broadcast Messages์˜ ๊ฒฝ์šฐ, ํ•˜์œ„ ๊ณ„์ธต์— ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ๋“ค๊นŒ์ง€ ์ œ์–ด๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ•จ์ˆ˜๋ช…์ด "On + Actions name"์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, "Move"๋ผ๋Š” Actions๊ฐ€ ์žˆ๋‹ค๋ฉด, ํ•จ์ˆ˜๋ช…์€ "OnMove"
  • ์œ„์™€ ๊ฐ™์ด ํ•จ์ˆ˜๋ช…์„ ๊ตฌ์„ฑํ•œ ํ›„ ํ•ด๋‹น Actions์˜ ํ‚ค ์ž…๋ ฅ์ด ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ, OnMove() ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๊ฐ„๋‹ค.

 

ํ˜„์žฌ Player ์บ๋ฆญํ„ฐ์— ์ ์šฉํ•˜๋„๋ก ๋งŒ๋“ค์–ด ๋†“์€ Action Map์—” "Move"์™€ "Jump" ์ด ์žˆ๋‹ค.

 

 

ํ•ด๋‹น Actions์—์„œ ๋ฐ›๋Š” ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด, On + Actions name ์ด๋ฆ„์ธ ํ•จ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.

void OnMove(InputValue value) // "Move" Actions์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค ์ž…๋ ฅ ์‹œ ์ž๋™ ํ˜ธ์ถœ
{
   ...
}

void OnJump()                 // "Jump" Actions์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค ์ž…๋ ฅ ์‹œ ์ž๋™ ํ˜ธ์ถœ
{
  // Jump๋Š” Action Type์„ "Button"์œผ๋กœ ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋‹ค.
  // ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ํ‚ค๊ฐ€ ์ž…๋ ฅ๋œ ๊ฒƒ, ํ˜ธ์ถœ๋˜์ง€ ์•Š์œผ๋ฉด ์ž…๋ ฅ๋˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
   ...
}

 

 

Tip) Action Type์ด "Value"์ธ ํ‚ค ํ˜ธ์ถœ์€ ์ž…๋ ฅ๊ณผ ์ž…๋ ฅ๋˜์ง€ ์•Š์„ ๋•Œ, ์ด๋ ‡๊ฒŒ ๋‘ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค.

๋”๋ณด๊ธฐ
void OnMove(InputValue value)
{
   Vector2 input = value.Get<Vector2>();
   // ์ด์ „ ๊ฒŒ์‹œ๊ธ€์„ ๋ณด๋ฉด, "Move" Actions์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค ๋ฆฌํ„ด์€ 'Vector2'๋กœ ๋ฐ›๊ธฐ๋กœ ํ–ˆ๋‹ค.
   
   Debug.Log($"SEND_MESSAGE : {input}");
}

 

์ƒํ•˜์ขŒ์šฐ ์•„๋ฌด ํ‚ค๋‚˜ ๋ˆŒ๋Ÿฌ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๊ฐ€ ๋‘ ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค.

 

Input Actions์˜ Actions์— ๋Œ€ํ•œ ํ‚ค Properties ์ค‘ Mode๋ฅผ "Digital Normalized"๋กœ ์„ค์ •ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ์ •๊ทœํ™”๋œ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.

 

์ฆ‰, ํ‚ค๋ฅผ ๋ˆ„๋ฅผ ๋•Œ๋Š” ํ•ด๋‹น ํ‚ค ์ž…๋ ฅ์— ๋Œ€ํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์ง€๊ณ , ํ‚ค๋ฅผ ๋‹ค์‹œ ๋—„ ๋•Œ ๋˜ํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์ง„๋‹ค.

 

์‹ค์Šต์œผ๋กœ OnMove() ํ•จ์ˆ˜ ํ•˜๋‚˜๋งŒ ๋กœ์ง์„ ์ž‘์„ฑํ•ด๋ณด์ž.

private Vector3 moveDirection;
private float moveSpeed = 4f;


void Update()
{
   bool hasControl = (moveDirection != Vector3.zero);
   if(hasControl)
   {
      transform.rotation = Quaternion.LookRotation(moveDirection);
      transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
   }
}


void OnMove(InputValue value)
{
     Vector2 input = value.Get<Vector2>();                   
     if(input != null)
     {
         moveDirection = new Vector3(input.x, 0f, input.y);
         Debug.Log($"SEND_MESSAGE : {input.magnitude}");
     }
}

 

์ด๋™ ๋กœ์ง์€ ์ž˜ ๊ตฌํ˜„๋์„ ๊ฒƒ์ด๋‹ค.

 

Send Messages๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ (์บ๋ฆญํ„ฐ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ๋”ฐ๋กœ ์„ค์ •ํ•œ ๊ฒ๋‹ˆ๋‹ค. ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.)

 

 

 

Invoke Unity Events ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

 

  • Unity์˜ Event ๋ฐฉ์‹์„ ์‚ฌ์šฉ
  • ํ•ด๋‹น Actions์— ๋Œ€ํ•œ ํ‚ค ์ž…๋ ฅ์ด ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ, ์„ค์ •ํ•œ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

 

์œ ๋‹ˆํ‹ฐ ์ด๋ฒคํŠธ ํ˜ธ์ถœ ๋ฐฉ์‹์ฒ˜๋Ÿผ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

"PlayerActions"์— ๋Œ€ํ•œ Actions ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋“ค ์„ค์ •

 

๊ธฐ์กด ๋กœ์ง์€ ๋˜‘๊ฐ™์œผ๋ฏ€๋กœ ํ•จ์ˆ˜๋งŒ Invoke Unity Events ๋ฐฉ์‹์— ๋งž๊ฒŒ ์กฐ๊ธˆ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.

public void OnMove(InputAction.CallbackContext context)
{
   Vector2 input = context.ReadValue<Vector2>();
   if(input != null)
   {
      moveDirection = new Vector3(input.x, 0f, input.y);
      Debug.Log($"UNITY_EVENTS : {input.magnitude}");
   }
}


public void OnJump(InputAction.CallbackContext context)
{
   if(context.performed)  // Action Type์ด "Button"์ผ ๊ฒฝ์šฐ ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ๋Š”์ง€ ์ฒดํฌ
   {
      // ์ ํ”„ ๋กœ์ง
   }
}

 

Send Messages์™€ ๋‹ฌ๋ฆฌ "On + Actions name"์œผ๋กœ ํ•จ์ˆ˜๋ช…์„ ์ง€์„ ํ•„์š”๋Š” ์—†์œผ๋‚˜, ์ด๋ฒคํŠธ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋„ค์ด๋ฐ ๊ทœ์น™, On์„ ์ ์šฉํ•ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ธ์ŠคํŽ™ํ„ฐ(Inspector) ์ฐฝ์—์„œ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์ด ์žˆ๋Š”๋ฐ, ํ•จ์ˆ˜์˜ ์ ‘๊ทผ์ง€์ •์ž๋ฅผ public์œผ๋กœ ํ•ด์ค˜์•ผ Inspector ์ฐฝ์—์„œ ์ด๋ฒคํŠธ ํ•จ์ˆ˜ ๋“ฑ๋ก์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฒคํŠธ ํ•จ์ˆ˜๋กœ, Player ์Šคํฌ๋ฆฝํŠธ์— ์žˆ๋Š” OnMove ํ•จ์ˆ˜ ๋“ฑ๋ก

 

์ ํ”„๋„ ๋˜‘๊ฐ™์ด ์ ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ๋ฒˆ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ดค๋‹ค.

 

Invoke Unity Events๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ

 

์ž˜ ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

ํ•„์š”ํ•œ ๋ถ„๋“ค์„ ์œ„ํ•œ Player ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด ์ฝ”๋“œ

๋”๋ณด๊ธฐ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
    private Animator _animator;
    private Vector3 moveDirection;
    private float moveSpeed = 4f;


    void Start()
    {
        _animator = this.GetComponent<Animator>();
    }

    void Update()
    {
        bool hasControl = (moveDirection != Vector3.zero);
        if (hasControl)
        {
            transform.rotation = Quaternion.LookRotation(moveDirection);
            transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
        }
    }

    #region SEND_MESSAGE
    void OnMove(InputValue value)
    {
        Vector2 input = value.Get<Vector2>();                 // ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ
        if(input != null)
        {
            moveDirection = new Vector3(input.x, 0f, input.y);
            _animator.SetFloat("moveSpeed", input.magnitude);   // magnitude๋Š” ๋ฒกํ„ฐ์˜ ๊ธธ์ด
            Debug.Log($"SEND_MESSAGE : {input.magnitude}");
        }
    }

    void OnJump()
    {
        // ์ ํ”„์— ๊ด€ํ•œ ๋กœ์ง
    }
    #endregion


    #region UNITY_EVENTS
    public void OnMove(InputAction.CallbackContext context)   // Unity Event๋กœ ๋ฐ›์„ ๊ฒฝ์šฐ
    {
        Vector2 input = context.ReadValue<Vector2>();
        if (input != null)
        {
            moveDirection = new Vector3(input.x, 0f, input.y);
            _animator.SetFloat("moveSpeed", input.magnitude); 
            Debug.Log($"UNITY_EVENTS : {input.magnitude}");
        }
    }

    public void OnJump(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            Debug.Log("Unity Event Jump");
        }
    }
    #endregion
}

 

 

์š”์•ฝ

 

  • Actions Map์˜ Actions์—์„œ ์„ค์ •ํ•œ ํ‚ค ์ž…๋ ฅ์„ ์Šคํฌ๋ฆฝํŠธ๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ Behavior๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Behavior๊ฐ€ ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ์€ Send Messages, Broadcast Messages, Invoke Unity Events, Invoke C sharp Events๊ฐ€ ์žˆ๋‹ค.
  • Send Messages, Broadcast Messages๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ํ•จ์ˆ˜๋ช…์„ On + Actions name์œผ๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ‚ค ์ž…๋ ฅ์— ๋Œ€ํ•ด ํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.
  • ์ด๋ฒคํŠธ(Event)๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ํ•จ์ˆ˜์˜ ์ ‘๊ทผ์ง€์ •์ž๋ฅผ public์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.
  • ์–ด๋–ค ๋ฐฉ์‹์„ ๊ณ ๋ฅผ์ง€๋Š” ๊ฐœ์ธ์ด ์„ ํ˜ธํ•˜๋Š” ์ทจํ–ฅ์— ๋”ฐ๋ผ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.
    • ๊ฐœ์ธ์ ์œผ๋กœ Send Messages, Broadcast Messages ๋ฐฉ๋ฒ•์€ C#์˜ ๋ฆฌํ”Œ๋ ‰์…˜(Reflection) ๊ธฐ๋Šฅ์— ์˜์กดํ•ด์„œ ์„ฑ๋Šฅ์— ๊ทธ๋ฆฌ ์ข‹์ง€ ์•Š๋‹ค๊ณ  ๋“ค์—ˆ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•