복습: 입력 시스템 전환
Coddy Unity 여정의 실제 게임 만들기 섹션에 포함된 레슨. 60개 중 48번째.
이 장에서는 Input System을 사용해 키보드, 마우스 및 프로젝트의 액션을 읽었습니다. 거품 지팡이는 여전히 기존 Input 클래스를 사용합니다. 호출을 하나씩 옮겨 보세요.
챌린지
중급버블 지팡이를 Input System으로 포팅하세요. 이전 Input 클래스는 여기서 오류를 발생시킵니다.
using UnityEngine.InputSystem;을 추가하고InputAction필드moveAction과jumpAction을 선언한 다음,Start()에서InputSystem.actions.FindAction을 사용해 설정하세요.moveAction.ReadValue<Vector2>()로 이동하세요. y 값은 z축을 따라갑니다.- Jump 검사를
WasPressedThisFrame(),IsPressed(),WasReleasedThisFrame()으로 바꾸고, 클릭 검사는Mouse.current.leftButton.wasPressedThisFrame으로 바꾸세요.
‘플레이’를 누르고, 거품을 불어 만든 다음 터뜨리세요. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using UnityEngine;
public class Wand : MonoBehaviour
{
public float speed = 4f;
public float growRate = 1f;
public float maxSize = 1.5f;
public GameObject bubblePrefab;
private Transform bubble;
void Update()
{
Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
transform.position += move.normalized * speed * Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
bubble = Instantiate(bubblePrefab, transform.position + Vector3.up * 1.5f, Quaternion.identity).transform;
}
if (Input.GetButton("Jump") && bubble != null)
{
float size = Mathf.Min(bubble.localScale.x + growRate * Time.deltaTime, maxSize);
bubble.localScale = Vector3.one * size;
}
if (Input.GetButtonUp("Jump") && bubble != null)
{
Debug.Log("Bubble: " + bubble.localScale.x.ToString("F1"));
bubble = null;
}
if (Input.GetMouseButtonDown(0))
{
GameObject[] bubbles = GameObject.FindGameObjectsWithTag("Bubble");
foreach (GameObject b in bubbles) Destroy(b);
Debug.Log("Popped " + bubbles.Length);
}
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
실제 게임 만들기의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드