Shooting Game チュートリアル 第04回
すすめていきます。
4.1 スクリプトの使い回し
はいはい、使い回しは得意です!…ということじゃないですよね。
そもそもいまさらですがコンポーネント=部品、構成要素。と確認。
そして今回作るSpaceship.csはEnemyとPlayer共通ってことね。
継承とは違う??
こちらもエラー出ます。例によって修正。↓
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;
}
}
OK。
次にPlayer.csを変更。
これは動かない。
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 5;
// public GameObject bullet;
Spaceship spaceship;
// private Rigidbody2D rb2;
private Transform bulletTransform;
void Start() {
// rb2 = GetComponent<Rigidbody2D>();//取得
spaceship = GetComponent<Spaceship>;
StartCoroutine ("FirstCoroutine");
}
IEnumerator FirstCoroutine ()
{
while (true) {
//bulletTransform = GetComponent<Transform>();
//Instantiate (bullet, bulletTransform.position, bulletTransform.rotation);
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;
// rb2.velocity = direction * speed;//使用
spaceship.Move (direction);
}
}
これで動きました。
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {public float speed = 5;
public GameObject bullet;
Spaceship spaceship;
// private Transform bulletTransform;void Start() {
spaceship = GetComponent<Spaceship>();
// bulletTransform = GetComponent<Transform>();
StartCoroutine ("FirstCoroutine");}
IEnumerator FirstCoroutine ()
{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);
}
}
bullet関係はSpaceship.csで書いてるから消しちゃうってことですね。
4.2 敵を表示する
問題なし。
4.3 敵専用のスクリプトを書く
今更わかってきたけど、インスタンス化するときに*をつけないんだね!
そしておそろしいことにエラー無しで動いた。
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 ("shotCoroutine");
}
IEnumerator shotCoroutine(){
while (true) {
for(int i = 0; i < tran.childCount; i++){
Transform shotPosition = tran.GetChild(i);
spaceship.Shot(shotPosition);
}
yield return new WaitForSeconds (spaceship.shotDelay);
}
}
このwhile文のtrueはどういうこと?
待機中ずっとっていう意味でいいのかな??
そんでそのまま進行。Enemy.csはwhile文の出前に入れ込んで。
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 ("shotCoroutine");
}
IEnumerator shotCoroutine(){
if (spaceship.canShot == false) {
yield break;
}
while (true) {
for(int i = 0; i < tran.childCount; i++){
Transform shotPosition = tran.GetChild(i);
spaceship.Shot(shotPosition);
}
yield return new WaitForSeconds (spaceship.shotDelay);
}
}
}
続きます。