21. スクリプトの編集:PlayerAction.cs
using UnityEngine;
using System.Collections;
public class PlayerAction : MonoBehaviour {
public Transform PlayerShotWeapon;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Instantiate(PlayerShotWeapon,
this.transform.position + new Vector3(0,1,0),
this.transform.rotation);
}
}
}
22. スクリプトの解説
if (Input.GetMouseButtonDown (0)) {
Instantiate(PlayerShotWeapon,
this.transform.position + new Vector3(0,1,0),
this.transform.rotation);
}
Input.GetMouseButtonDown(0)
マウスの左ボタン(0番ボタン)の状態を返す
Instantiate関数
指定されたオブジェクトをScene上に動的に生成する
この場合はPlayerShotWeapon(弾)をプレイヤーの座標から少し上にずらした位置とプレイヤーの向いている方向に揃えて配置
する
PlayerShotWeaponは自動で前方向に移動するため、このスクリプトでは動的に生成するところまででOK
31. スクリプトの編集:EnemyAction.cs
using UnityEngine;
using System.Collections;
public class EnemyAction : MonoBehaviour {
public Transform explosion;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider _other){
Destroy (gameObject);
Instantiate (explosion, this.transform.position, Quaternion.identity);
}
}
46. スクリプトの編集:EnemySpawnPoint.cs
using UnityEngine;
using System.Collections;
public class EnemySpawnPoint : MonoBehaviour {
public Transform enemyCharacter;
// Use this for initialization
void Start () {
StartCoroutine (spawnEnemy ());
}
// Update is called once per frame
void Update () {
}
IEnumerator spawnEnemy(){
while (true) {
Instantiate (enemyCharacter,transform.position,Quaternion.identity);
yield return new WaitForSeconds (
Random.Range (2, 5)
);
}
}
}