Unity 6 부터는 기본으로 Input System Package만 사용
ball player 둘다 rigidbody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
float moveSpeed = 5f;
Vector3 dir = Vector3.zero;
public GameObject ballPrefab;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
dir = new Vector3(moveX, 0, moveZ).normalized;
rb.velocity = new Vector3(dir.x * moveSpeed, 0, dir.z * moveSpeed);
if (Input.GetButtonDown("Fire1"))
{
GameObject ball = Instantiate(ballPrefab, transform.position, Quaternion.identity);
ball.GetComponent<Rigidbody>().AddForce(dir * 10f, ForceMode.Impulse);
}
}
}
기존 방식의 이동 및 총알 발사 코드
였었다
InputSystem 사용
Player에 Player Input 추가
create Actions 누르면 뜸
뜬다
모바일 / vr / 조이스틱 등 어떤 컨트롤러여도 움직임을 구현할 수 있게
behavior가 send message일때 위에 코드 구현
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
float moveSpeed = 5f;
Vector3 dir = Vector3.zero;
public GameObject ballPrefab;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
rb.velocity = new Vector3(dir.x * moveSpeed, 0, dir.y * moveSpeed);
}
public void OnMove(InputValue value) //Input System에 있는 기본 함수임 자동완성 안됨 조심
{
dir = value.Get<Vector2>();
}
public void OnFire(InputValue value)
{
if (value.isPressed)
{
GameObject ball = Instantiate(ballPrefab, transform.position, Quaternion.identity);
ball.GetComponent<Rigidbody>().AddForce(dir * 10f, ForceMode.Impulse);
}
}
}
내부적으로 발생하는 이벤트들을 메세지 처리 기반 방식으로 사용하면 스택으로 쌓임
아주 중요하고 우선순위가 높은건 100개 쌓여있을 때 제일 앞에다 끼워져 넣을 수 있음 (post message)
그래서 send messages 보다는 invoke unity events를 사용하는게 더 주류
send messages: monobehaviour 메시지 함수 호출 (소규모 게임에 사용해라)
invoke unity events: 인스펙터에서 이벤트 연결 방식 (디자인이나 여러명이서 사용할 때 써라)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
float moveSpeed = 5f;
Vector3 dir = Vector3.zero;
public GameObject ballPrefab;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
rb.velocity = new Vector3(dir.x * moveSpeed, 0, dir.y * moveSpeed);
}
public void OnMove(InputAction.CallbackContext context)
{
if(context.performed) { dir = context.ReadValue<Vector2>(); }
else if (context.canceled) { dir = Vector2.zero; }
}
public void OnFire(InputAction.CallbackContext context)
{
if (context.performed)
{
GameObject ball = Instantiate(ballPrefab, transform.position, Quaternion.identity);
ball.GetComponent<Rigidbody>().AddForce(dir * 10f, ForceMode.Impulse);
}
}
}
Invoke Unity Events 방식으로 구현한 코드
728x90
반응형
'공부 > Unity' 카테고리의 다른 글
[Unity] XR interaction toolkit과 UI 사용하기 / VR 한글 키보드 (2) | 2025.07.04 |
---|---|
[Unity] XR Toolkit Interaction (0) | 2025.06.27 |
[Unity] 컴포넌트 안쓰고 위치, 애니메이션 연동 (Oculus + Photon) (1) | 2025.06.24 |
[Unity] Photon Network 포톤 네트워크 Lobby manager 예시 (2) | 2025.06.24 |
[Unity] Oculus Integration / 컨트롤러 연결 및 조준선 표시 (2) | 2025.06.19 |