unityゲームチュートリアルspace shooter(破棄)
3324 ワード
unityをよりよく理解するために、チュートリアルを模倣しました.以下はチュートリアルのコードです.
DestroyByBoundary:
DestroyByContact.cs:
DestroyByBoundary:
DestroyByContact.cs:
DestroyByTime .cs:
DestroyByBoundary:
using UnityEngine;
using System.Collections;
public class DestroyByBoundary : MonoBehaviour
{
void OnTriggerExit (Collider other)
{
Destroy(other.gameObject);
}
}
DestroyByContact.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExpolsion;
public int score;
private GameController gameController;
void Start(){
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if(gameControllerObject != null){
gameController = gameControllerObject.GetComponent ();
}
if(gameControllerObject == null){
Debug.Log("can not find 'GameController'script");
}
}
void OnTriggerEnter(Collider other){
if(other.tag=="Boundary"){
return;
}
Instantiate (explosion, transform.position, transform.rotation);
if(other.tag=="Player"){
Instantiate (playerExpolsion,other.transform.position,other.transform.rotation);
gameController.GameOver ();
}
gameController.addScore (score);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
DestroyByBoundary:
using UnityEngine;
using System.Collections;
public class DestroyByBoundary : MonoBehaviour
{
void OnTriggerExit (Collider other)
{
Destroy(other.gameObject);
}
}
DestroyByContact.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExpolsion;
public int score;
private GameController gameController;
void Start(){
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if(gameControllerObject != null){
gameController = gameControllerObject.GetComponent ();
}
if(gameControllerObject == null){
Debug.Log("can not find 'GameController'script");
}
}
void OnTriggerEnter(Collider other){
if(other.tag=="Boundary"){
return;
}
Instantiate (explosion, transform.position, transform.rotation);
if(other.tag=="Player"){
Instantiate (playerExpolsion,other.transform.position,other.transform.rotation);
gameController.GameOver ();
}
gameController.addScore (score);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
DestroyByTime .cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByTime : MonoBehaviour {
public float lifeTime;
// Use this for initialization
void Start () {
Destroy (gameObject,lifeTime);
}
// Update is called once per frame
void Update () {
}
}