Layer | Z Position |
0 - Background | 10 |
1 - Middleground | 5 |
2 - Foreground | 0 |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using UnityEngine;
/// <summary> /// Player controller and behavior /// </summary> public class PlayerScript : MonoBehaviour { /// <summary> /// 1 - The speed of the ship /// </summary> public Vector2 speed = new Vector2( 50, 50); // 2 - Store the movement private Vector2 movement; void Update() { // 3 - Retrieve axis information float inputX = Input.GetAxis( "Horizontal"); float inputY = Input.GetAxis( "Vertical"); // 4 - Movement per direction movement = new Vector2( speed.x * inputX, speed.y * inputY); } void FixedUpdate() { // 5 - Move the game object rigidbody2D.velocity = movement; } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using UnityEngine;
/// <summary> /// Simply moves the current game object /// </summary> public class MoveScript : MonoBehaviour { // 1 - Designer variables /// <summary> /// Object speed /// </summary> public Vector2 speed = new Vector2( 10, 10); /// <summary> /// Moving direction /// </summary> public Vector2 direction = new Vector2(- 1, 0); private Vector2 movement; void Update() { // 2 - Movement movement = new Vector2( speed.x * direction.x, speed.y * direction.y); } void FixedUpdate() { // Apply movement to the rigidbody rigidbody2D.velocity = movement; } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using UnityEngine;
/// <summary> /// Projectile behavior /// </summary> public class ShotScript : MonoBehaviour { // 1 - Designer variables /// <summary> /// Damage inflicted /// </summary> public int damage = 1; /// <summary> /// Projectile damage player or enemies? /// </summary> public bool isEnemyShot = false; void Start() { // 2 - Limited time to live to avoid any leak Destroy(gameObject, 20); // 20sec } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using UnityEngine;
/// <summary> /// Handle hitpoints and damages /// </summary> public class HealthScript : MonoBehaviour { /// <summary> /// Total hitpoints /// </summary> public int hp = 1; /// <summary> /// Enemy or player? /// </summary> public bool isEnemy = true; /// <summary> /// Inflicts damage and check if the object should be destroyed /// </summary> /// <param name="damageCount"></param> public void Damage( int damageCount) { hp -= damageCount; if (hp <= 0) { // Dead! Destroy(gameObject); } } void OnTriggerEnter2D(Collider2D otherCollider) { // Is this a shot? ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>(); if (shot != null) { // Avoid friendly fire if (shot.isEnemyShot != isEnemy) { Damage(shot.damage); // Destroy the shot Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script } } } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using UnityEngine;
/// <summary> /// Launch projectile /// </summary> public class WeaponScript : MonoBehaviour { //-------------------------------- // 1 - Designer variables //-------------------------------- /// <summary> /// Projectile prefab for shooting /// </summary> public Transform shotPrefab; /// <summary> /// Cooldown in seconds between two shots /// </summary> public float shootingRate = 0.25f; //-------------------------------- // 2 - Cooldown //-------------------------------- private float shootCooldown; void Start() { shootCooldown = 0f; } void Update() { if (shootCooldown > 0) { shootCooldown -= Time.deltaTime; } } //-------------------------------- // 3 - Shooting from another script //-------------------------------- /// <summary> /// Create a new projectile if possible /// </summary> public void Attack( bool isEnemy) { if (CanAttack) { shootCooldown = shootingRate; // Create a new shot var shotTransform = Instantiate(shotPrefab) as Transform; // Assign position shotTransform.position = transform.position; // The is enemy property ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>(); if (shot != null) { shot.isEnemyShot = isEnemy; } // Make the weapon shot always towards it MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>(); if (move != null) { move.direction = this.transform.right; // towards in 2D space is the right of the sprite } } } /// <summary> /// Is the weapon ready to create a new projectile? /// </summary> public bool CanAttack { get { return shootCooldown <= 0f; } } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void Update()
{ // ... // 5 - Shooting bool shoot = Input.GetButtonDown( "Fire1"); shoot |= Input.GetButtonDown( "Fire2"); // Careful: For Mac users, ctrl + arrow is a bad idea if (shoot) { WeaponScript weapon = GetComponent<WeaponScript>(); if (weapon != null) { // false because the player is not an enemy weapon.Attack( false); } } // ... } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using UnityEngine;
/// <summary> /// Enemy generic behavior /// </summary> public class EnemyScript : MonoBehaviour { private WeaponScript weapon; void Awake() { // Retrieve the weapon only once weapon = GetComponent<WeaponScript>(); } void Update() { // Auto-fire if (weapon != null && weapon.CanAttack) { weapon.Attack( true); } } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System.Collections.Generic;
using UnityEngine; /// <summary> /// Enemy generic behavior /// </summary> public class EnemyScript : MonoBehaviour { private WeaponScript[] weapons; void Awake() { // Retrieve the weapon only once weapons = GetComponentsInChildren<WeaponScript>(); } void Update() { foreach (WeaponScript weapon in weapons) { // Auto-fire if (weapon != null && weapon.CanAttack) { weapon.Attack( true); } } } } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//PlayerScript.cs //.... void OnCollisionEnter2D(Collision2D collision) { bool damagePlayer = false; // Collision with enemy EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript>(); if (enemy != null) { // Kill the enemy HealthScript enemyHealth = enemy.GetComponent<HealthScript>(); if (enemyHealth != null) enemyHealth.Damage(enemyHealth.hp); damagePlayer = true; } // Damage the player if (damagePlayer) { HealthScript playerHealth = this.GetComponent<HealthScript>(); if (playerHealth != null) playerHealth.Damage( 1); } } |