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

第04回

Spaceship.csの作成

  • Rigidbody2Dを使用前に取得する(Moveメソッドはpublicなので外部から呼び出される。Startメソッドが呼ばれる前に呼び出される可能性があるのでStartメソッド内での取得は行わない)。
using UnityEngine;
using System.Collections;

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

    public void Shot (Transform origin) {
        Instantiate(bullet, origin.position, origin.rotation);
    }

    public void Move(Vector2 direction)
    {
        Rigidbody2D rb2 = GetComponent<Rigidbody2D> ();
        rb2.velocity = direction * speed; //使用
    }
}
  • Rigidbody2DとTransformを前もって取得しておく。
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    Spaceship spaceship;
    public float speed = 5;
    public GameObject bullet;
    private Transform tran; //宣言

    void Start() {
        tran = GetComponent<Transform>(); //取得
        StartCoroutine("IE");
    }

    IEnumerator IE()
    {
        spaceship = GetComponent<Spaceship>();
        while (true) {
            spaceship.Shot(tran);
            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);
    }
}

spaceship取得はStartメソッド内でもコルーチン内でもOK。オリジナル重視でここでは後者。

敵の移動

  • Transformを使用前に取得する。
using UnityEngine;
using System.Collections;

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

    void Start () {
        spaceship = GetComponent<Spaceship> ();
        tran = GetComponent<Transform> (); //取得
        spaceship.Move (tran.up * -1); //使用
    }
}

弾を撃たない敵を作る

  • Rigidbody2Dを使用前に取得する。
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 void Shot (Transform origin) {
        Instantiate(bullet, origin.position, origin.rotation);
    }

    public void Move(Vector2 direction)
    {
        Rigidbody2D rb2 = GetComponent<Rigidbody2D> (); //宣言&取得
        rb2.velocity = direction * speed; //使用
    }
}
  • Transformを前もって取得しておく。
using UnityEngine;
using System.Collections;

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

    void Start () {
        spaceship = GetComponent<Spaceship> ();
        tran = GetComponent<Transform> ();
        spaceship.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);
        }
    }
}