Trois balles
Leçon 17 sur 19 du cours Jeu Unity : casse-briques de Coddy.
Un bonus Multiball divise la balle : deux exemplaires s’envolent depuis l’endroit où elle se trouve. Instantiate copie un objet de scène comme il copie un prefab, mais seuls les éléments que l’Inspector enregistrerait sont copiés : les champs publics et ceux marqués [SerializeField], comme speed et paddle. Un champ privé comme launched est initialisé à false, et la vélocité à zéro ; une copie doit donc être lancée, sinon elle reste sur la raquette.
Ball copy = Instantiate(source, source.transform.position, Quaternion.identity);
copy.Launch(new Vector3(0.2f, 1f, 0f));GameManager conserve toutes les balles en jeu dans une List<Ball>, afin de pouvoir les retrouver plus tard.
Défi
DifficileAttrape un bonus Multiball et la balle se divise en trois.
- Dans
GameManager, ajouteList<Ball> balls = new List<Ball>();(avecusing System.Collections.Generic;) ; Start() y ajoute d’abordball. - Écris
public void SplitBall(): deux copies deballs[0]à sa position, lancées vers(-0.6f, 1f, 0f)et(0.6f, 1f, 0f), toutes deux ajoutées à balls. - Dans
PowerUp, un bonus Multiball appelleGameManager.Instance.SplitBall().
Appuie sur « Jouer » et attrape-en un rose : trois balles s’envolent. Puis clique sur « Vérifier ».
Essayez vous-même
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;
}
}
Toutes les leçons de Jeu Unity : casse-briques
Entraînez-vous par vous-même : Playground Unity