66. spawnerスクリプト
スクリプトはこ
ちら
https://github.com/nyar
urato/kzunity_hands_on/
blob/master/hans_on01/
Assets/Scripts/spawner.c
s
using UnityEngine;
using System.Collections;
public class spawner : MonoBehaviour {
public GameObject bullet_prefab, camera_obj;
public float shoot_force;
private GameObject bullet_clone;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)){
bullet_clone = Instantiate(bullet_prefab,
gameObject.transform.position,
gameObject.transform.rotation) as
GameObject;
Vector3 shoot_vector = new Vector3(0,0,1);
shoot_vector =
camera_obj.transform.rotation * shoot_vector;
bullet_clone.rigidbody.AddForce(shoot_force * shoot_vector);
Destroy(bullet_clone,10f);
}
}
}
79. Scoreのスクリプト
スクリプト
https://github.com/nyarurato/k
zunity_hands_on/blob/master/h
ans_on01/Assets/Scripts/score.c
s
using UnityEngine;
using System.Collections;
public class score : MonoBehaviour {
public GUIStyle score_style;
private int score_value;
// Use this for initialization
void Start () {
score_value = 0;
}
// Update is called once per frame
void Update () {
}
public void AddScore(int value){
score_value += value;
}
void OnGUI(){
Rect rect = new Rect(0, 0, 100, 200);
string str = "Score : " +
score_value.ToString();
GUI.Label(rect, str, score_style);
}
}
83. Enemy_spawnerのスクリプト
スクリプト
https://github.com/nyarurato/kz
unity_hands_on/blob/master/han
s_on01/Assets/Scripts/enemy_spa
wner.cs
using UnityEngine;
using System.Collections;
public class enemy_spawner : MonoBehaviour {
public GameObject enemy_prefab;
public float area_value;
// Use this for initialization
void Start () {
StartCoroutine("spawn");
}
// Update is called once per frame
void Update () {
}
IEnumerator spawn(){
while(true){
Vector3 pos = new
Vector3(Random.value * area_value,
3,
Random.value * area_value);
pos +=
gameObject.transform.position;
Instantiate(enemy_prefab,
pos,
gameObject.transform.rotation);
yield return new
WaitForSeconds(3f);
}
}
88. 的(Enemy)のスクリプト
スクリプト
https://github.com/nyarurato/kzun
ity_hands_on/blob/master/hans_on
01/Assets/Scripts/enemy.cs
using UnityEngine;
using System.Collections;
public class enemy : MonoBehaviour {
public int enemyscore;
GameObject score_obj;
score score_component;
// Use this for initialization
void Start () {
score_obj = GameObject.Find("Score");
score_component =
score_obj.GetComponent<score>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision){
if(collision.gameObject.tag == "bullet"){
score_component.AddScore(enemyscore);
Destroy(gameObject);
}
}
}