UNITY3D学习笔记6

时间:2020-12-08 21:40:43



using UnityEngine;
using System.Collections;

public class TestA9 : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		Ray ray = new Ray(Vector3.zero,transform.position);

		RaycastHit hit;

		Physics.Raycast(ray,out hit,100);
		Debug.DrawLine(ray.origin,hit.point);
	}
}



using UnityEngine;
using System.Collections;

public class TestA8 : MonoBehaviour {

	private CharacterController controller = null;
	private float moveSpeed = 3.0f;
	private float rotateSpeed = 3.0f;

	private GameObject colliderObj = null;
	// Use this for initialization
	void Start () {
		controller = GetComponent<CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		if(GUILayout.RepeatButton("left")){
			transform.Rotate(0,-rotateSpeed,0);
		}

		if(GUILayout.RepeatButton("right")){
			transform.Rotate(0,rotateSpeed,0);
		}

		if(GUILayout.RepeatButton("front"))
		{
			Vector3 forward = transform.TransformDirection(Vector3.forward);
			controller.Move(forward*moveSpeed);
		}

		if(GUILayout.RepeatButton("back"))
		{
			Vector3 forward = transform.TransformDirection(Vector3.forward);
			controller.Move(forward*-moveSpeed);
		}

		if(GUILayout.RepeatButton("up")){
			transform.Translate(0,1,0);
		}

		
		if(GUILayout.RepeatButton("down")){
			transform.Translate(0,-1,0);
		}

		if(controller.collisionFlags == CollisionFlags.Sides){
			if(colliderObj!=null){
				GUI.color = Color.black;
				GUI.Label(new Rect(200,100,200,100),"碰撞对象为:"+colliderObj.name);
			}
		}
	}

	void OnControllerColliderHit(ControllerColliderHit hit){
		colliderObj = hit.gameObject;
		print(11);
	}
}



using UnityEngine;
using System.Collections;

public class TestA7 : MonoBehaviour {

	private CharacterController controller = null;
	private float moveSpeed = 30.0f;
	private float rotateSpeed = 3.0f;

	// Use this for initialization
	void Start () {
		controller = GetComponent<CharacterController>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){

		if(GUILayout.RepeatButton("left")){
			transform.Rotate(0,-rotateSpeed,0);
		}

		if(GUILayout.RepeatButton("right")){
			transform.Rotate(0,rotateSpeed,0);
		}

		if(GUILayout.RepeatButton("forward"))
		{
			controller.SimpleMove(Vector3.forward * moveSpeed);
		}

		if(GUILayout.RepeatButton("back"))
		{
			controller.SimpleMove(Vector3.forward * -moveSpeed);
		}

		if(GUILayout.RepeatButton("to left"))
		{
			controller.SimpleMove(Vector3.right * -moveSpeed);
		}

		if(GUILayout.RepeatButton("to right"))
		{
			controller.SimpleMove(Vector3.right * moveSpeed);
		}
	}
}



using UnityEngine;
using System.Collections;

public class TestA6 : MonoBehaviour {
	string show = null;
	// Use this for initialization
	void Start () {
		show = "wei fa sheng peng zhuang";
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnCollisionEnter(Collision collision){
		show = "JinRuPengZhuang:"+collision.gameObject.name;
	}

	void OnCollisionStay(Collision collision){
		show = "PengZhuangZhong:"+collision.gameObject.name;
	}

	void OnCollisionExit(Collision collision){
		show = "PengZhuangJieSu:"+collision.gameObject.name;
		collision.gameObject.rigidbody.Sleep();
	}

	void OnGUI(){
		GUILayout.Label(show);
	}
}



using UnityEngine;
using System.Collections;

public class TestA5 : MonoBehaviour {

	GameObject addFrceObj = null;
	GameObject addPosObj = null;
	GameObject cubeObj = null;
	GameObject cam1;
	// Use this for initialization
	void Start () {
		addFrceObj = GameObject.Find("Sphere1");
		addPosObj = GameObject.Find("Sphere2");
		cubeObj= GameObject.Find("Cube");
		cam1 = GameObject.Find("Camera");

		cam1.active = false;
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI()
	{
		if(GUILayout.Button("putongli",GUILayout.Height(50))){
			addFrceObj.rigidbody.AddForce(1000,0,1000);
		}

		if(GUILayout.Button("weizili",GUILayout.Height(50))){
			Vector3 force = cubeObj.transform.position - addPosObj.transform.position;
			addPosObj.rigidbody.AddForceAtPosition(force,addPosObj.transform.position,ForceMode.Impulse);
			cam1.active = true;
		}
	}
}



using UnityEngine;
using System.Collections;

public class TestA1 : MonoBehaviour {

	// Use this for initialization
	void Start () {
		int a = Random.Range(0,100);
		float b = Random.Range(0.0f,10.0f);

		Debug.Log("int:"+a);

		Debug.Log("float:"+b);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}



using UnityEngine;
using System.Collections;

public class TestA2 : MonoBehaviour {

	bool isRotation = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(isRotation){
			gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation,
			                                                 Quaternion.Euler(0.0f,50.0f,0.0f),
			                                                 Time.time * 0.1f);
		}
	}

	void OnGUI(){
		if(GUILayout.Button("rotation degree",GUILayout.Height(50))){
			gameObject.transform.rotation = Quaternion.Euler(0.0f,50.0f,0.0f);
		}

		if(GUILayout.Button("fixed degree",GUILayout.Height(50))){
			isRotation = true;
		}
	}
}



using UnityEngine;
using System.Collections;

public class TestA3 : MonoBehaviour {

	GameObject plane;
	GameObject cube;

	float mapWidth;
	float mapHeight;

	float widthCheck;
	float heightCheck;

	float mapcube_x = 0;
	float mapcube_y = 0;

	bool keyUP;
	bool keyDown;
	bool keyLeft;
	bool keyRight;

	public Texture map;
	public Texture map_cube;

	// Use this for initialization
	void Start () {
		plane = GameObject.Find("Plane");
		cube = GameObject.Find("Cube");

		float size_x = plane.GetComponent<MeshFilter>().mesh.bounds.size.x;
		float scal_x = plane.transform.localScale.x;

		float size_z = plane.GetComponent<MeshFilter>().mesh.bounds.size.z;
		float scal_z = plane.transform.localScale.z;

		mapWidth = size_x*scal_x;
		mapHeight = size_z*scal_z;

		widthCheck = mapWidth /2;
		heightCheck = mapHeight/2;

		check();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		keyUP = GUILayout.RepeatButton("keyUP"); 
		keyDown = GUILayout.RepeatButton("keyDown");
		keyLeft = GUILayout.RepeatButton("keyLeft");
		keyRight = GUILayout.RepeatButton("keyRight");

		GUI.DrawTexture(new Rect(Screen.width - map.width,0,map.width,map.height),map);
		GUI.DrawTexture(new Rect(mapcube_x,mapcube_y,map_cube.width,map_cube.height),map_cube);
	}

	void FixedUpdate(){
		if(keyUP){
			cube.transform.Translate(Vector3.forward*Time.deltaTime*5);
			check();
		}

		if(keyDown)
		{
			cube.transform.Translate(-Vector3.forward*Time.deltaTime*5);
			check();
		}

		if(keyLeft){
			cube.transform.Translate(-Vector3.right*Time.deltaTime*5);
			check();
		}

		if(keyRight){
			cube.transform.Translate(Vector3.right*Time.deltaTime*5);
			check();
		}
	}

	void check(){
		float x = cube.transform.position.x;
		float z = cube.transform.position.z;

		if(x>=widthCheck)
		{
			x = widthCheck;
		}
		if(x<= -widthCheck){
			x = -widthCheck;
		}
		if(z>=heightCheck){
			z = heightCheck;
		}
		if(z<=-heightCheck){
			z = -heightCheck;
		}

		cube.transform.position = new Vector3(x,cube.transform.position.y,z);

		mapcube_x = (map.width/mapWidth * x) +((map.width/2)-(map_cube.width/2))+(Screen.width - map.width);

		mapcube_y = map.height-((map.height/mapHeight*z)+(map.height/2));
	}
}