您好,欢迎来到刀刀网。
搜索
您的当前位置:首页unity射线

unity射线

来源:刀刀网

接上一博文:


1,小球表示碰撞交点

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  

public class RayCast : MonoBehaviour {  
	
	[HideInInspector]  
	private List<GameObject> hitGObs;   
	private GameObject headPointer;  
	private GameObject findColor;  


	//save the old color and objects
	private Dictionary<string, Color> ObjectsColor;
	//获取场景中的物体
	private GameObject GOB;

	void Start () {    


		headPointer = new GameObject(); 
		headPointer = GameObject.Find ("HeadPointer");
		if (headPointer) {
			headPointer.GetComponent<MeshRenderer> ().enabled = false;
		} else {
			Debug.LogError ("cannot find object of HeadPointer");
		}
			

		hitGObs = new List<GameObject>(1);    
		hitGObs.Clear ();     


		//保存场景中物体的颜色和名称
		ObjectsColor = new Dictionary<string, Color> ();
		SaveObjectColor ();

		GOB = new GameObject ();
	
	}    

	// Update is called once per frame    
	void Update () {    

		//如果写为transform.forward则表示线是无限长    

		Ray ray=new Ray(transform.position,transform.forward*100);  //定义一个射线对象,包含射线发射的位置transform.position,发射距离transform.forward*100;    
		//drawline in the scene  
		Debug.DrawLine(transform.position,transform.position+transform.forward*100,Color.red);  //这个就是绘制出的射线了,包含发射位置,发射距离和射线的颜色;    
		RaycastHit hitInfo;                                 //定义一个RaycastHit变量用来保存被撞物体的信息;    

		//如果碰撞到了物体,hitInfo里面就包含该物体的相关信息;  
		if (Physics.Raycast (ray, out hitInfo, 100)) {          
		
			DrawLineInGame (hitInfo.point);  

			GameObject hitme = hitInfo.collider.gameObject;  
			//hitInfo.point:碰撞点的位置; 
			//hitInfo.normal:与碰撞点所在平面垂直的向量;    
			//hitInfo.collider.gameobject:可以得到该物体上的所有信息了;  
			if (hitme.name != "VideoStream" && hitme.name != "Table" && hitme.name != "HeadPointer") 
			{
				hitme.GetComponent<MeshRenderer> ().material.color = Color.green;    
				//put the hitting objects into the array:hitGObs,以便使其颜色能回到以前的状态    
				hitGObs.Add (hitme); 
			} else {
					ResetOldColor (hitme);
				   }
		} 
		else 
		{   
		  ResetOldColor ();
		}    

	}    

	void DrawLineInGame (Vector3 point)  
	{  

		//render the position of sphere
		headPointer.GetComponent<MeshRenderer> ().enabled = true;
		headPointer.transform.position = point;



	}  


	//save the whole objects and color
	void SaveObjectColor()
	{
		findColor = GameObject.Find ("01_JiZuo");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("02_FangKuaiLuoMu");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("03_HuoDongQianShen");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("04_LuoGan");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("05_LuoDing");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("06_HuKouBan");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("07_LuoDing_1");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("07_LuoDing_2");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("07_LuoDing_3");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("07_LuoDing_4");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("08_LuoMu");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

		findColor = GameObject.Find ("09_HuKouBan");
		ObjectsColor.Add (findColor.name,findColor.GetComponent<MeshRenderer>().material.color);

	}


	//有碰撞时物体颜色的还原
	void ResetOldColor(GameObject hitme)
	{
		foreach (var item in hitGObs) {  
			foreach (var item_color in ObjectsColor) {

				if (item_color.Key == item.name) {
					item.GetComponent<MeshRenderer> ().material.color = ObjectsColor[item_color.Key];
				}
			}
		}
		hitGObs.Clear ();
	}

	void ResetOldColor()
	{
		//这一句将决定小球从物体边界到无穷远处时小球是否会停留在物体边缘感觉不自然
		headPointer.GetComponent<MeshRenderer> ().enabled = false;

		foreach (var item in ObjectsColor) {
			GOB = GameObject.Find (item.Key);
			GOB.GetComponent<MeshRenderer>().material.color = ObjectsColor[item.Key];
		}
	
	}

	void OnDisable()
	{
		Destroy (GOB);

	}
} 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务