Shooting Game チュートリアル 第09回
すすめていきます。
そうそう気になってたの。プレイヤーがはみだして消えるの。
9.1 プレイヤーに移動制限つける
今回は移動の制限にカメラのビューポートとMathf.Clampを使用します。
Mathf.clampってどういう意味?
Mathfとは数学的な関数を使用するクラス。clampは固定とかそういう意味。ということで範囲制限をMathf.Clampで行うということらしいと理解。
9.2 Spaceship.csとEnemy.csの修正
そうだった、Moveメソッドを消さないと。
というわけで修正修正。
Enemy.csはこうした。
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
Spaceship spaceship;
private Transform tran;
private Rigidbody2D rb2;
void Start () {
spaceship = GetComponent<Spaceship>();
tran = GetComponent<Transform> ();
rb2 = GetComponent<Rigidbody2D>();
// spaceship.Move (tran.up * -1);
Move (transform.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);
}
}
public void Move (Vector2 direction){
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);
}
}
続きます。