UNITY3D學習筆記16



using UnityEngine;
using System.Collections;

public class Test1 : MonoBehaviour {

	public int a1 = 0;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}



using UnityEngine;
using System.Collections;

public class Test2 : MonoBehaviour {

	Test1 mTest1;

	// Use this for initialization
	void Start () {
		mTest1 = new Test1();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		if(GUILayout.Button("abc",GUILayout.Width(100))){
			Debug.Log(mTest1.a1);
		}

		if(GUILayout.Button("abc2",GUILayout.Width(100))){
			//Debug.Log(mTest1.a1);
			mTest1.a1++;
		}

	}
}



using UnityEngine;
using System.Collections;

public class Test3 : MonoBehaviour {

	float x,z;
	public int speed = 5;   

	// Use this for initialization
	void Start () {
	
	}
	
	// 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);   
	}
}



using UnityEngine;
using System.Collections;

public class Test4 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnDrawGizmos()  
	{  
		Gizmos.DrawIcon(this.transform.position, "2.tif");  
	}  
}



using UnityEngine;
using System.Collections;

public class Test5 : MonoBehaviour {

	public Transform m_transform;
	CharacterController m_ch;

	float m_movSpeed = 3.0f;

	float m_gravity = 2.0f;

	public int m_life = 5;

	//--攝像機---------------------------------------
	Transform m_camTransform;
	Vector3 m_camRot;
	float m_camHeight = 1.4f;



	// Use this for initialization
	void Start () {
		m_transform = this.transform;
		m_ch = this.GetComponent<CharacterController>();

		//--攝像機---------------------------------------
		m_camTransform = Camera.main.transform;
		Vector3 pos = m_transform.position;
		pos.y += m_camHeight;
		m_camTransform.position = pos;

		m_camTransform.rotation = m_transform.rotation;
		m_camRot = m_camTransform.eulerAngles;

		Screen.lockCursor = true;
	}
	
	// Update is called once per frame
	void Update () {
		if(m_life<=0)
			return;

		Control();
	}

	void Control(){

		float xm = 0,ym = 0,zm = 0;

		ym -= m_gravity*Time.deltaTime;

		if(Input.GetKey(KeyCode.W)){
			zm += m_movSpeed * Time.deltaTime;
		}else if(Input.GetKey(KeyCode.S)){
			zm -= m_movSpeed * Time.deltaTime;
		}

		if(Input.GetKey(KeyCode.A)){
			xm -= m_movSpeed * Time.deltaTime;
		}else if(Input.GetKey(KeyCode.D)){
			xm += m_movSpeed * Time.deltaTime;
		}
	
		m_ch.Move(m_transform.TransformDirection(new Vector3(xm,ym,zm)));

		//--攝像機---------------------------------------
		float rh = Input.GetAxis("Mouse X");
		float rv = Input.GetAxis("Mouse Y");

		m_camRot.x -= rv;
		m_camRot.y += rh;
		m_camTransform.eulerAngles = m_camRot;

		Vector3 camrot = m_camTransform.eulerAngles;
		camrot.x = 0;
		camrot.z = 0;
		m_transform.eulerAngles = camrot;


		Vector3 pos = m_transform.position;
		pos.y += m_camHeight;
		m_camTransform.position = pos;
	}

	void OnDrawGizmos(){
		Gizmos.DrawIcon(this.transform.position, "2.tif");  
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章