UNITY3D学习笔记2

时间:2022-11-22 21:38:03


using UnityEngine;
using System.Collections;

public class Test1 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//this.transform.Translate(new Vector3(0,0.2f,0));
	}
}



using UnityEngine;
using System.Collections;

public class Test2 : MonoBehaviour {

	int speed = 5;
	float x,z;
	public Transform newobject; 
	bool bLeft,bRight; 
	public GameObject gb1;
	public Texture texture;

	float sh = Screen.height;
	float sw = Screen.width;

	// Use this for initialization
	void Start () {
		//print (1);
		//newobject = GameObject.Find("qiu1");
		bLeft = false;
		bRight= false;
	}
	
	// Update is called once per frame
	void Update () {
		//x = Input.GetAxis("Horizontal")*Time.deltaTime * speed;
		//z = Input.GetAxis("Vertical")*Time.deltaTime * speed;

		//this.transform.Translate(x,0,z);
		if(bLeft){
			this.transform.Translate(x-(5*Time.deltaTime),0,z);
		}
		if(bRight){
			this.transform.Translate(x+(5*Time.deltaTime),0,z);
		}


		if(Input.GetButton("Fire1")){
			//shot();
		}

		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);
		}
	}

	void OnGUI(){

		if(GUI.Button(new Rect(sw-100,sh-50,100,50),"射击")){
			shot();
		}

		if(GUI.Button(new Rect(0,sh-100,100,100),"左")){ 
			//this.transform.Translate(x-(5*Time.deltaTime),0,z);
			if(bLeft){
				bLeft = false;
			}else{
				bLeft = true;
				bRight = false;
			}
		}

		if(GUI.Button(new Rect(100,sh-100,100,100),"右")){
			//this.transform.Translate(x+(5*Time.deltaTime),0,z);
			if(bRight){
				bRight = false;
			}else{
				bRight = true;
				bLeft = false;
			}
		}
	}

	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 Test3 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(gameObject.transform.position.y<0){
			Destroy(gameObject);
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test4 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		this.transform.Translate(new Vector3(0,0.2f,0));
	}
}



using UnityEngine;
using System.Collections;

public class Test5 : MonoBehaviour {
	int speed = 5;
	int i1 = 0;
	int i2 = 0;
	int i3 = 0;

	float i12 = 1;
	float i22;

	public Transform newobject; 

	// 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++;
			//print (i1);
			i2 = 0;
		}

		if(i12>i22){
			shot();
			i12 = 0;
		}else{
			i12 = i12 + Time.deltaTime;
			//print (i12);
		} 


	}

	void OnCollisionEnter(Collision collision){
		print ("OK"+i3);
		print (collision.collider.name);
		i3++;
	}

	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 Test6 : MonoBehaviour {
	float i1 = 1;
	float i2;
	
	public Transform newobject; 

	// Use this for initialization
	void Start () {
		i2 = Random.Range(1,5);
	}
	
	// Update is called once per frame
	void Update () {
		if(i1>i2){
			shot();
			i1 = 0;
		}else{
			i1 = i1 + Time.deltaTime;
			//print (i1);
		}
	}

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

}