unity游戏教程 space shooter (销毁)

为了更好地理解unity,模仿了教程,以下均为教程中的代码:

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<GameController> ();
        }
        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<GameController> ();
        }
        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 () {
        
    }
}

转载于:https://www.cnblogs.com/1997Ff/p/7364702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值