Introduction à Unity
1 min de lecture
Unity est l’un des moteurs de jeu les plus populaires au monde. Il permet de créer des jeux 2D, 3D, VR et AR pour de nombreuses plateformes.
Pourquoi Unity ?
Section intitulée « Pourquoi Unity ? »- Multiplateforme : PC, consoles, mobile, web, VR/AR
- Communauté : Large communauté et Asset Store
- C# : Langage de programmation accessible
- Gratuit : Version gratuite pour les petits studios
Installation
Section intitulée « Installation »- Téléchargez Unity Hub
- Installez Unity Hub
- Ajoutez une version LTS de Unity (ex: 2022.3 LTS)
- Installez Visual Studio ou VS Code pour l’édition de code
Interface de Unity
Section intitulée « Interface de Unity »┌─────────────────────────────────────────────────────────────┐│ Menu Bar │├────────────┬─────────────────────────┬──────────────────────┤│ │ │ ││ Hierarchy │ Scene View │ Inspector ││ │ │ ││ (Objets │ (Vue de la scène) │ (Propriétés de ││ de la │ │ l'objet sélect.) ││ scène) ├─────────────────────────┤ ││ │ Game View │ ││ │ (Vue du joueur) │ │├────────────┴─────────────────────────┴──────────────────────┤│ Project / Console ││ (Fichiers du projet / Logs) │└─────────────────────────────────────────────────────────────┘Concepts fondamentaux
Section intitulée « Concepts fondamentaux »GameObjects et Components
Section intitulée « GameObjects et Components »Tout dans Unity est un GameObject auquel on attache des Components :
GameObject "Player"├── Transform (position, rotation, scale)├── Sprite Renderer (affichage 2D)├── Rigidbody2D (physique)├── Collider2D (collisions)└── PlayerController (script personnalisé)Premier script C#
Section intitulée « Premier script C# »Créez un script PlayerController.cs :
using UnityEngine;
public class PlayerController : MonoBehaviour{ [SerializeField] private float speed = 5f;
private Rigidbody2D rb;
void Start() { // Appelé une fois au démarrage rb = GetComponent<Rigidbody2D>(); }
void Update() { // Appelé chaque frame float horizontal = Input.GetAxisRaw("Horizontal"); float vertical = Input.GetAxisRaw("Vertical");
Vector2 movement = new Vector2(horizontal, vertical).normalized; rb.velocity = movement * speed; }}Cycle de vie des scripts
Section intitulée « Cycle de vie des scripts »public class GameLoop : MonoBehaviour{ void Awake() { /* Initialisation avant Start */ } void Start() { /* Initialisation */ } void Update() { /* Chaque frame */ } void FixedUpdate(){ /* Physique, intervalle fixe */ } void LateUpdate() { /* Après Update, caméra */ } void OnDestroy() { /* Nettoyage */ }}Physique 2D basique
Section intitulée « Physique 2D basique »// Détecter les collisionsvoid OnCollisionEnter2D(Collision2D collision){ if (collision.gameObject.CompareTag("Enemy")) { Debug.Log("Touché par un ennemi !"); TakeDamage(10); }}
// Détecter les triggers (zones sans collision physique)void OnTriggerEnter2D(Collider2D other){ if (other.CompareTag("Collectible")) { CollectItem(other.gameObject); Destroy(other.gameObject); }}