using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public int speed = 10; void Start () { GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed; } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] public class Spaceship : MonoBehaviour { public float speed; // 移動スピード public float shotDelay; // 弾を撃つ間隔 public GameObject bullet; // 弾のPrefab public bool canShot; // 弾を撃つかどうか // 弾の作成 public void Shot (Transform origin) { Instantiate (bullet, origin.position, origin.rotation); } // 機体の移動 public void Move (Vector2 direction) { GetComponent<Rigidbody2D>().velocity = direction * speed; } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Spaceship spaceship; IEnumerator Start () { spaceship = GetComponent<Spaceship> (); while (true) { spaceship.Shot (transform); yield return new WaitForSeconds (spaceship.shotDelay); } } void Update () { float x = Input.GetAxisRaw ("Horizontal"); float y = Input.GetAxisRaw ("Vertical"); Vector2 direction = new Vector2 (x, y).normalized; spaceship.Move (direction); } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { Spaceship spaceship; IEnumerator Start () { spaceship = GetComponent<Spaceship> (); spaceship.Move (transform.up * -1); // Y軸のマイナス方向に移動する if (spaceship.canShot == false) { yield break; } while (true) { for (int i = 0; i < transform.childCount; i++) { Transform shotPosition = transform.GetChild(i); spaceship.Shot (shotPosition); } yield return new WaitForSeconds (spaceship.shotDelay); } } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] public class Spaceship : MonoBehaviour { public float speed; // 移動スピード public float shotDelay; // 弾を撃つ間隔 public GameObject bullet; // 弾のPrefab public bool canShot; // 弾を撃つかどうか public GameObject explosion; // 爆発のPrefab // 爆発の作成 public void Explosion () { Destroy (gameObject, 0.25f); Instantiate (explosion, transform.position, transform.rotation); } // 弾の作成 public void Shot (Transform origin) { Instantiate (bullet, origin.position, origin.rotation); } // 機体の移動 public void Move (Vector2 direction) { GetComponent<Rigidbody2D>().velocity = direction * speed; } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Spaceship spaceship; IEnumerator Start () { spaceship = GetComponent<Spaceship> (); while (true) { spaceship.Shot (transform); yield return new WaitForSeconds (spaceship.shotDelay); } } void Update () { float x = Input.GetAxisRaw ("Horizontal"); float y = Input.GetAxisRaw ("Vertical"); Vector2 direction = new Vector2 (x, y).normalized; spaceship.Move (direction); } // 衝突すると呼び出される void OnTriggerEnter2D (Collider2D c) { string layerName = LayerMask.LayerToName(c.gameObject.layer); Debug.Log("Player:" + layerName); // 弾の削除 Destroy(c.gameObject); // 爆発する spaceship.Explosion(); // プレイヤーを削除 Destroy (gameObject); } } |
Player:Default Enemy:Default |
Layer | ID |
---|---|
User Layer 8 | Player |
User Layer 9 | Enemy |
User Layer 10 | Bullet(Player) |
// 衝突すると呼び出される void OnTriggerEnter2D (Collider2D c) { string layerName = LayerMask.LayerToName(c.gameObject.layer); Debug.Log("Player: " + layerName); if( layerName == "Enemy") { // 弾の削除 Destroy(c.gameObject); // プレイヤーを削除 Destroy (gameObject); } } |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { Spaceship spaceship; IEnumerator Start () { spaceship = GetComponent<Spaceship> (); spaceship.Move (transform.up * -1); if (spaceship.canShot == false) { yield break; } while (true) { for (int i = 0; i < transform.childCount; i++) { Transform shotPosition = transform.GetChild(i); spaceship.Shot (shotPosition); } yield return new WaitForSeconds (spaceship.shotDelay); } } void OnTriggerEnter2D (Collider2D c) { string layerName = LayerMask.LayerToName(c.gameObject.layer); Debug.Log("Enemy: " + layerName); if (layerName == "Player" || layerName == "Bullet(Player)") { Destroy(c.gameObject); spaceship.Explosion(); Destroy(gameObject); } } } |
[Next Chapter ↓] Shooting-06
[Previous Chapter ↑] Shooting-04