シューティングチュートリアルをUnity5で(第09回)

第09回

プレイヤーの移動方法を変更

チュートリアルの説明文で

つまり一度移動範囲外に出た後、範囲内に強制的に移動する事になります。

とあるので「一回画面外に消えた後、強制的に戻って来る」動作をするのかと思ったけど実際の動作は消えたきり戻ってこない。 後に

プレイヤーが削除されるはずです。

と書いてあった。

  • 特記事項なし。
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    Spaceship spaceship;
    private Transform tran;
    private AudioSource audioSource;
    void Start() {
        spaceship = GetComponent<Spaceship>();
        tran = GetComponent<Transform>();
        audioSource = GetComponent<AudioSource> ();
        StartCoroutine("IE");
    }

    IEnumerator IE()
    {
        while (true) {
            spaceship.Shot(tran);
            audioSource.Play ();
            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);
        Clamp();
    }

    void Clamp() {
        Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
        Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
        Vector2 pos = tran.position;
        Debug.Log (max.x + "-" + max.y);
        pos.x = Mathf.Clamp (pos.x, min.x, max.x);
        pos.y = Mathf.Clamp (pos.y, min.y, max.y);

        transform.position = pos;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName(c.gameObject.layer);
        if (layerName == "Bullet(Enemy)") {
            Destroy (c.gameObject);
        }

        if (layerName == "Bullet(Enemy)" || layerName == "Enemy") {
            spaceship.Explosion ();
            Destroy (gameObject);
        }
    }
}
  • 特記事項なし。
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    Spaceship spaceship;
    private Transform tran;
    private AudioSource audioSource;
    void Start() {
        spaceship = GetComponent<Spaceship>();
        tran = GetComponent<Transform>();
        audioSource = GetComponent<AudioSource> ();
        StartCoroutine("IE");
    }

    IEnumerator IE()
    {
        while (true) {
            spaceship.Shot(tran);
            audioSource.Play ();
            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;
        Move(direction);
    }

    void Move(Vector2 direction) {
        Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
        Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
        Vector2 pos = transform.position;

        pos += direction * spaceship.speed * Time.deltaTime;

        pos.x = Mathf.Clamp (pos.x, min.x, max.x);
        pos.y = Mathf.Clamp (pos.y, min.y, max.y);

        transform.position = pos;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName(c.gameObject.layer);
        if (layerName == "Bullet(Enemy)") {
            Destroy (c.gameObject);
        }

        if (layerName == "Bullet(Enemy)" || layerName == "Enemy") {
            spaceship.Explosion ();
            Destroy (gameObject);
        }
    }
}

9.2 Spaceship.csとEnemy.csの修正

  • 特記事項なし
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]
public class Spaceship : MonoBehaviour {
    public float speed; 
    public float shotDelay;
    public GameObject bullet;
    public bool canShot;
    public GameObject explosion;

    public void Explosion()
    {
        Transform tran = GetComponent<Transform> ();
        Instantiate (explosion, tran.position, tran.rotation);
    }

    public void Shot (Transform origin) {
        Instantiate(bullet, origin.position, origin.rotation);
    }
}
  • Rigidbody2を使用時に取得。
using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
    Spaceship spaceship;
    private Transform tran;

    void Start () {
        spaceship = GetComponent<Spaceship> ();
        tran = GetComponent<Transform> ();
        Move (tran.up * -1);

        StartCoroutine ("IE");
    }

    IEnumerator IE() {
        if (spaceship.canShot == false) {
            yield break;
        }

        while (true) {
            for (int i = 0; i < tran.childCount; i++) {
                Transform shotPosition = transform.GetChild (i);
                spaceship.Shot (shotPosition);
            }
            yield return new WaitForSeconds (spaceship.shotDelay);
        }
    }

    public void Move(Vector2 direction)
    {
        Rigidbody2D rb2 = GetComponent<Rigidbody2D> ();
        rb2.velocity = direction * spaceship.speed;
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName (c.gameObject.layer);

        if (layerName != "Bullet(Player)")
            return;

        Destroy (c.gameObject);
        spaceship.Explosion ();
        Destroy (gameObject);
    }
}