- Unity์์ ํค๋ณด๋,๋ง์ฐ์ค, ์ฝ์ ๋ฑ ๋ค์ํ ํ๋ซํผ์ ํตํด ์
๋ ฅ์ ๋ฐ์ ์ ์๊ฒ ์ง์ํ๋ ์์คํ
์
๋๋ค.
- ์ ๋ํฐ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํ๋ Input Manager์ ๋จ์ ์ ๋ณด์ํฉ๋๋ค.
- GetKey(), "Horizontal" ๊ณผ ๊ฐ์ ์ถ ์
๋ ฅ์ ํตํด ์ฝ๊ฒ ์
๋ ฅ๊ฐ์ ๋ฐ์ ์ ์์ต๋๋ค.
- ๋ค๋ฅธ ํ๋ซํผ์ผ๋ก ๊ฒ์์ ๋ง๋๋ ๊ฒฝ์ฐ, ์ ์ฝ๋๋ฅผ ์ผ์ผ์ด ์์ ํด์ผ ํฉ๋๋ค.
public class Move : MonoBehavior
{
Rigidbody rigid;
void Start ()
{
rigid = GetComponent();
}
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.getAxis("Vertical");
rigid.velocity = new Vector2(x,y);
}
}
- Input System์ ๊ฒฝ์ฐ, Scheme ๋ง ๋ณ๊ฒฝํด์ฃผ๋ฉด ํ๋ซํผ์ ๋ฐ๋ฅธ ์ฝ๋๋ฅผ ์ฝ๊ฒ ์์ฑํ ์ ์์ต๋๋ค.
- Unity์์ Window -> Package Manager๋ฅผ ํด๋ฆญํฉ๋๋ค.
- Packages:In Project ๋ฅผ Packages:Unity Registry ๋ก ๋ณ๊ฒฝ, Input System์ ๊ฒ์ํฉ๋๋ค.
- Install ๋ฒํผ์ ๋๋ฅธ ํ ํ๋ก์ ํธ๋ฅผ ์ฌ์์ํฉ๋๋ค.
- Edit -> Project Setting -> Player -> Active Input Handling ๋ฅผ ์ฐพ์ต๋๋ค.
- Input System Package(New)์ ์ ํํฉ๋๋ค. Input Manager๋ ์ฐ๊ณ ์ถ๋ค๋ฉด Both๋ฅผ ์ ํํฉ๋๋ค.
- ์ดํ Project -> Create ์์ Input Action Asset๋ฅผ ์์ฑํ์ฌ Input System์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- Control Scheme๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋ซํผ์ ๋ฐ๋ฅธ ์
๋ ฅ๋ฐฉ์์ ์ฝ๊ฒ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
- Inspector -> Open ์ ํตํด Control Scheme๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
- ์ด๋ฆ๊ณผ ์
๋ ฅ๋ฐฉ์์ ์ค์ ํ์ฌ Control Scheme๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- ์
๋ ฅ์ฅ์น๋ฅผ ํตํด ์
๋ ฅ์ ์ด๋ค๋ฐฉ๋ฒ์ผ๋ก, ์ด๋ค ๊ฐ์ผ๋ก ๋ฐ์์ฌ์ง๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
- Action Properties -> Action Type, Control Type๋ฅผ ํตํด ์ด๋ค ๊ฐ์ ๋ฐ์ ์ง ์ค์ ํ ์ ์์ต๋๋ค.
- Binding์ ์ถ๊ฐ, Binding๋ง๋ค Path๋ฅผ ์ค์ ํ์ฌ ์ด๋ค ํค๋ก ์ด๋ค ์
๋ ฅ์ ๋ฐ์์ง ์ค์ ํ ์ ์์ต๋๋ค.
- save๋ฒํผ์ผ๋ก ์ค์ ๊ฐ์ ์ ์ฅํ ์ ์์ต๋๋ค.
- Player Input ์ปดํฌ๋ํธ๋ฅผ ํตํด Input Action Asset๋ฅผ ์ค๋ธ์ ํธ์ ์ ์ฉํ ์ ์์ต๋๋ค.
- Behavior ์ต์
์ ๋ฐ๋ผ ์คํฌ๋ฆฝํธ ์์ฑ๋ฒ์ด ๋ฌ๋ผ์ง๋๋ค.
- On+(Action์ ์ด๋ฆ) ํํ๋ก ํจ์์ด๋ฆ์ ์์ฑํฉ๋๋ค.
- InputValue๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๊ฐ๋ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Move_sendM : MonoBehaviour
{
Rigidbody rigid;
Vector2 MoveDir = new Vector3(1f,1f,0f);
void Start()
{
rigid = GetComponent();
}
void Update()
{
rigid.velocity = MoveDir; // ์๋ ์กฐ์
}
public void OnMove(InputValue Val) // ํค๋ฅผ ์
๋ ฅ๋ฐ์์ ๋ Val์ ๋ฐ์ ๋ฒกํฐ๋ฅผ ์ป์ด๋.
{
Vector2 vec = Val.Get(); // ์
๋ ฅ ๋ฐ์ ๋ฒกํฐ ๊ฐ์ ์ ์ฅ
if (vec != null)
MoveDir = new Vector3(vec.x, vec.y,0f); // ๋ฌผ์ฒด๊ฐ ๊ฐ์ง ๋ฒกํฐ
}
}
- ํจ์์ ์ด๋ฆ์ ๋ง์๋๋ก ๊ฒฐ์ ํ ์ ์์ต๋๋ค.
- CallbackContext๋ฅผ ํตํด ์
๋ ฅ์ ๋ฐ์์ต๋๋ค.
- ์ดํ ์ค์ ์์ ์ค๋ธ์ ํธ๋ฅผ Event์ ์ถ๊ฐํ ํ, ์๋์ํฌ ํจ์๋ฅผ ์ค์ ํด์ผํฉ๋๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Move_Event : MonoBehaviour
{
Rigidbody rigid;
Vector3 MoveDir;
void Start()
{
rigid = GetComponent();
}
public void OnMove(InputAction.CallbackContext context) // ๊ฐ์ ์
๋ ฅ๋ฐ๋ ํจ์
{
Vector2 vec = context.ReadValue();
if (vec != null)
MoveDir = new Vector3(vec.x, vec.y, 0f);
}
void Update()
{
rigid.velocity = MoveDir;
}
}
