• 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 () {
    		
    	}
    }
    
  • 相关阅读:
    css最简单的在背景图片上显示模糊背景色的方法
    css添加网格背景
    获取bing必应图片
    JavaScript超过一定高度导航添加类名
    6行css就可以解决的瀑布流布局的方法
    css实现背景图横向滚动
    JavaScript根据一个元素的显示隐藏控制另一个元素的显示和隐藏
    JavaScript判断地址栏链接与导航栏链接是否一致并给导航添加class
    JavaScript实现选中文字自动复制
    Day 74 算法基础(二)
  • 原文地址:https://www.cnblogs.com/1997Ff/p/7364702.html
Copyright © 2020-2023  润新知