Üç Top
Coddy'nin Unity Oyunu: Tuğla Kırma kursunda ders 17 / 19.
Çoklu Top güçlendirmesi topu ikiye böler: iki kopya bulunduğu yerden uçar. Instantiate, bir sahne nesnesini prefab'ı kopyalar gibi kopyalar; ancak yalnızca Inspector'ın kaydedeceği şeyler aktarılır: speed ve paddle gibi public ve [SerializeField] alanlar. launched gibi private bir alan false olarak başlar ve hız sıfır olur; bu nedenle kopyanın fırlatılması gerekir, yoksa raketin üzerinde kalır.
Ball copy = Instantiate(source, source.transform.position, Quaternion.identity);
copy.Launch(new Vector3(0.2f, 1f, 0f));GameManager, oyundaki tüm topları bir List<Ball> içinde tutar; böylece daha sonra onları bulabilir.
Görev
ZorÇoklu Top güçlendiricisini yakala; top üçe bölünür.
GameManageriçineList<Ball> balls = new List<Ball>();ekle (using System.Collections.Generic;ile); Start() önceballöğesini listeye ekler.public void SplitBall()yaz:balls[0]öğesinin bulunduğu konumda iki kopyasını oluştur, bunları(-0.6f, 1f, 0f)ve(0.6f, 1f, 0f)yönlerine fırlat ve ikisini de balls listesine ekle.PowerUpiçinde Çoklu Top,GameManager.Instance.SplitBall()çağırır.
“Oyna” düğmesine bas ve pembe olanı yakala: üç top uçar. Ardından “Kontrol et”.
Kendin dene
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public Brick brickPrefab;
public Transform wall;
public string[] levels;
public float spacingX = 1.75f;
public float spacingY = 0.75f;
public float topY = 6f;
public Ball ball;
public int lives = 3;
public TMP_Text scoreText;
public TMP_Text livesText;
public GameObject gameOverPanel;
public GameObject[] powerUps;
public float dropChance = 0.15f;
int score;
int level;
int bricksLeft;
void Awake()
{
Instance = this;
}
void Start()
{
BuildLevel(0);
ShowHud();
}
void BuildLevel(int index)
{
level = index;
string[] rows = levels[index].Split('/');
for (int row = 0; row < rows.Length; row++)
{
string line = rows[row];
float left = -(line.Length - 1) * spacingX / 2f;
for (int col = 0; col < line.Length; col++)
{
char c = line[col];
if (c == '.') continue;
Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
brick.SetHits(c - '0');
bricksLeft++;
}
}
}
public void BrickBroken(Brick brick)
{
score += brick.points;
bricksLeft--;
ShowHud();
if (Random.value < dropChance)
{
GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
Instantiate(prefab, brick.transform.position, Quaternion.identity);
}
if (bricksLeft == 0)
{
ball.ResetToPaddle();
BuildLevel((level + 1) % levels.Length);
}
}
public void BallLost(Ball lost)
{
lives--;
ShowHud();
if (lives == 0)
{
gameOverPanel.SetActive(true);
lost.gameObject.SetActive(false);
}
else lost.ResetToPaddle();
}
public void PlayAgain()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void ShowHud()
{
scoreText.text = "Score: " + score;
livesText.text = "Lives: " + lives;
}
}
Unity Oyunu: Tuğla Kırma bölümündeki tüm dersler
Kendi başına pratik yap: Unity playground