Menu
CoddyTech

Recapitulação: troca de entrada

Parte da seção Criando Jogos de Verdade do Journey de Unity da Coddy. Lição 48 de 60.

Neste capítulo, você leu o teclado, o mouse e as ações do projeto usando o Input System. O bastão de bolhas ainda usa a classe antiga Input: migre cada chamada individualmente.

challenge icon

Desafio

Médio

Adapte a varinha de bolhas para o Input System; a classe antiga Input gera um erro aqui.

  1. Adicione using UnityEngine.InputSystem; e os campos InputAction moveAction e jumpAction, definidos em Start() com InputSystem.actions.FindAction.
  2. Movimente-se com moveAction.ReadValue<Vector2>(), usando o y no eixo z.
  3. Troque os testes de Jump por WasPressedThisFrame(), IsPressed() e WasReleasedThisFrame(), e o clique por Mouse.current.leftButton.wasPressedThisFrame.

Pressione “Jogar”, sopre uma bolha e estoure-a. Depois, “Verificar”.

Experimente você mesmo

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);
        }
    }
}
quiz iconTeste seus conhecimentos

Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.

Todas as lições de Criando Jogos de Verdade

Pratique por conta própria: Playground de Unity