Κάπως έτσι λειτουργεί ο κώδικας στον Unity (2D shooter) και όχι με ένα κεντρικό “main” αρχείο. Δεν υπάρχει ένα συγκεκριμένο σημείο έναρξης και λήξης όπως σε μια κονσόλα C ή Java. Κάθε MonoBehaviour Script εκτελείται σύμφωνα με τον κύκλο ζωής του Unity (Awake, Start, Update κ.λπ.), και ο ροή του παιχνιδιού συντονίζεται από τα δικά σου scripts (π.χ. GameManager).
Τι σημαίνει στην πράξη για το πού αρχίζει και πού τελειώνει ο κώδικας:
Προτείνω μια απλή αρχή για να φτιάξεις ένα πρωτότυπο (prototype):
Παράδειγμα αρχικού κώδικα (μικρά, βασικά scripts που μπορείς να τα αντιγράψεις)
PlayerController.cs using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5f; public Transform firePoint; public GameObject bulletPrefab; public float fireCooldown = 0.25f; private float nextFire;
void Update() { // Μετακίνηση float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector2 move = new Vector2(h, v).normalized * speed * Time.deltaTime; transform.Translate(move);
// Στόχος με το ποντίκι
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 dir = (new Vector2(mouseWorld.x, mouseWorld.y) - (Vector2)transform.position).normalized;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
// Πυροβόλο
if (Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireCooldown;
Instantiate(bulletPrefab, firePoint.position, transform.rotation);
}
} }
Bullet.cs using UnityEngine; public class Bullet : MonoBehaviour { public float speed = 12f; public float lifeTime = 2f;
void Start() { Destroy(gameObject, lifeTime); } void Update() { transform.Translate(Vector3.right * speed * Time.deltaTime); }
void OnTriggerEnter2D(Collider2D other) { Mikel m = other.GetComponent<Mikel>(); if (m != null) { m.TakeDamage(25); Destroy(gameObject); } } }
Mikel.cs using UnityEngine; public class Mikel : MonoBehaviour { public int maxHealth = 100; private int currentHealth; public AudioClip deathAudio; private bool isAlive = true;
void Start() { currentHealth = maxHealth; }
public void TakeDamage(int dmg) { if (!isAlive) return; currentHealth -= dmg; if (currentHealth <= 0) Die(); }
void Die() { isAlive = false; Collider2D col = GetComponent<Collider2D>(); if (col != null) col.enabled = false;
if (deathAudio != null) {
AudioSource.PlayClipAtPoint(deathAudio, transform.position);
} else {
// Εναλλακτικά: εμφανίστε κείμενο/floating text με τη φράση
}
// Ενημέρωση GameManager
GameManager.Instance?.OnMikelDied();
Destroy(gameObject, 0.2f);
} }
GameManager.cs (singleton για ευκολία) using UnityEngine; public class GameManager : MonoBehaviour { public static GameManager Instance; public GameObject mikelPrefab; public int startCount = 6; private int remaining;
void Awake() { if (Instance == null) Instance = this; else Destroy(gameObject); DontDestroyOnLoad(gameObject); }
void Start() { SpawnMikels(startCount); }
void SpawnMikels(int count) { remaining = count; for (int i = 0; i < count; i++) { Vector2 pos = GetSpawnPosition(); Instantiate(mikelPrefab, pos, Quaternion.identity); } }
Vector2 GetSpawnPosition() { // Απλό εύρημα θέσης εντός ορισμένων ορίων return new Vector2(Random.Range(-8f, 8f), Random.Range(-4f, 4f)); }
public void OnMikelDied() { remaining--; if (remaining <= 0) { Debug.Log("Level complete!"); // Μετάβαση σε επόμενο επίπεδο ή οθόνη νίκης } } }
Πώς θα τα συνδέσεις:
Συμβουλές:
Αν θες, μπορώ να σου δώσω βήμα-βήμα οδηγίες για το συγκεκριμένο engine που θες (Unity, Godot, GameMaker) ή να προσαρμόσω τον κώδικα σε έκδοση C# ή GDScript αντίστοιχα. Επίσης, αν θέλεις, μπορώ να σου δώσω πιο “ready-to-paste” κώδικα για το επόμενο βήμα (π.χ. πλήρη κώδικα για GameManager με επίπεδα, ή ενσωμάτωση UI).
Loading...