zoukankan      html  css  js  c++  java
  • 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 () {
    		
    	}
    }
    
  • 相关阅读:
    如何在Unity中播放影片
    C# typeof()实例详解
    unity3d用鼠标拖动物体的一段代码
    unity3d中Find的用法
    geometry_msgs/PoseStamped 类型的变量的构造
    c++ ros 计算两点距离
    C++ 利用指针和数组以及指针和结构体实现一个函数返回多个值
    C++ 结构体指针的定义
    Cannot initialize a variable of type 'Stu *' with an rvalue of type 'void *'
    C++中的平方、开方、绝对值怎么计算
  • 原文地址:https://www.cnblogs.com/1997Ff/p/7364702.html
Copyright © 2011-2022 走看看