Menu
CoddyTech

Tekrar: Girdi Değişimi

Coddy'nin Unity Journey'sinin Gerçek Oyunlar Geliştirmek bölümünün bir parçası. Ders 48 / 60.

Bu bölümde Input System ile klavyeyi, fareyi ve projenin eylemlerini okudun. Baloncuk çubuğu hâlâ eski Input sınıfını kullanıyor: onu çağrı çağrı taşı.

challenge icon

Görev

Orta

Baloncuk çubuğunu Input System'e taşı; eski Input sınıfı burada hata verir.

  1. using UnityEngine.InputSystem; ekle ve moveAction ile jumpAction adlı InputAction alanlarını Start() içinde InputSystem.actions.FindAction ile ayarla.
  2. moveAction.ReadValue<Vector2>() ile yürü; y değeri z ekseni boyunca hareket eder.
  3. Jump kontrollerini WasPressedThisFrame(), IsPressed() ve WasReleasedThisFrame() ile, tıklama kontrolünü de Mouse.current.leftButton.wasPressedThisFrame ile değiştir.

“Oyna” düğmesine bas, bir baloncuk üfle ve patlat. Ardından “Kontrol et” düğmesine bas.

Kendin dene

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 iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Gerçek Oyunlar Geliştirmek bölümündeki tüm dersler

Kendi başına pratik yap: Unity playground