UNITY3D学习笔记3


using UnityEngine;
using System.Collections;

public class TestD3 : MonoBehaviour {
	public Transform newobject;  
	int speed = 5;  
	int i1 = 0;  
	int i2 = 0;  
	int i3 = 0;  
	
	float i12 = 1;  
	float i22;
	// Use this for initialization
	void Start () {
		i22 = Random.Range(1,5);  
	}
	
	// Update is called once per frame
	void Update () {
		if(i1> 500){  
			transform.Translate(speed*Time.deltaTime,0,0);  
			if(i2> 500){  
				i1 = 0;  
			}  
			i2++;   
		}else{  
			transform.Translate(-speed*Time.deltaTime,0,0);  
			i1++;   
			i2 = 0;  
		}   
		if(i12>i22){  
			shot();  
			i12 = 0;  
		}else{  
			i12 = i12 + Time.deltaTime;   
		}    
	}

	void shot(){  
		Transform n = Instantiate(newobject,transform.position,transform.rotation)as Transform;  
		Vector3 fwd = transform.TransformDirection(Vector3.forward);  
		n.rigidbody.AddForce(fwd*2800);  
	}  
}



using UnityEngine;
using System.Collections;

public class TestD2 : MonoBehaviour {
	int i1 = 0;
	string s1 = "";
	public GUIText GT2;
	// Use this for initialization
	void Start () {
		i1 = 99;
		GT2.text = "大方块生命值:"+i1;
	}
	
	// Update is called once per frame
	void Update () {
		//transform.Translate(Vector3.forward * Time.deltaTime);
		transform.Rotate(Vector3.up* Time.deltaTime*150);
	}

	void OnTriggerEnter(Collider col){
		print (col.collider.name);
	}

	void OnCollisionEnter(Collision col){
		
		//print (col.collider.name);
		s1 = col.collider.name;
		if(s1.Equals("qiu1(Clone)")){
			//print ("OK");
			i1--;
			//print(i1);
			GT2.text = "大方块生命值:"+i1;
			if(i1<0)
			{
				Destroy(gameObject);  
			}
		}
	}
}



using UnityEngine;
using System.Collections;

public class TestD1 : MonoBehaviour {
	int speed = 5;  
	float i1,x,z;
	public Transform cubeT; 
	public Transform newobject;
	public GUIText GT1;
	// Use this for initialization
	void Start () {
		//GT1 = GameObject.Find("/GT1");
		//GT1.text = "AAA";
	}
	
	// Update is called once per frame
	void Update () {
		i1 = Vector3.Distance(transform.position,cubeT.position);
		//print (i1);

		GT1.text = "距离:"+i1.ToString();

		x = Input.GetAxis("Horizontal")*Time.deltaTime * speed;  
		//z = Input.GetAxis("Vertical")*Time.deltaTime * speed;  

		this.transform.Translate(x,0,z);  

		if(Input.GetKey(KeyCode.Q))  
		{  
			transform.Rotate(0,-25*Time.deltaTime,0,Space.Self);  
		}  
		
		if(Input.GetKey(KeyCode.E))  
		{  
			transform.Rotate(0,25*Time.deltaTime,0,Space.Self);  
		}  

		if(Input.GetKey(KeyCode.Space)){
			//print ("fire");
			shot();
		}
	}

	void OnCollisionEnter(Collision col){

		print (col.collider.name);
	}

	void shot(){  
		Transform n = Instantiate(newobject,transform.position,transform.rotation)as Transform;  
		Vector3 fwd = transform.TransformDirection(Vector3.forward);  
		n.rigidbody.AddForce(fwd*2800);  
	}  
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值