栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

unity碰撞检测变色的简单应用

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity碰撞检测变色的简单应用

一开始物体带有初始颜色,

碰撞到物体时变色(分别有黄色和蓝色)

离开被碰撞物体时变色(红色,黑色,白色顺序切换)

public class SE : MonoBehaviour {

	int a = 2;
	int b = 3;
		void OnTriggerEnter(Collider other)//触发器可以穿透,所以把重力去掉
									   // OnTriggerEnter:触发接触的那一帧被调用  
		{
        	if (other.CompareTag("1"))//被碰撞的物体标签是(1)
        	{
                if (a%2==0)
               {
        		other.GetComponent().material.color = Color.yellow;
        				//被碰撞物体,添加组件<网格渲染>	材质球的颜色	变成  黄色
        	   }
                else if (a%2==1)
               {
        		other.GetComponent().material.color = Color.blue;
        	   }
        		a++;					//实现颜色切换
        		Debug.Log("enter");			//同等于print("")输出
        		Debug.Log(other.gameObject.name);       //同等于print("")输出	
        			  //other.gameObject.name 被碰撞物体的名字
        	}	
		 }
        void OnTriggerExit(Collider other)		//  触发结束被调用  
    	{
                if (b%3==0)
                {
        			other.GetComponent().material.color = Color.red;
        		}
                else if(b%3==1)
                {
        			other.GetComponent().material.color = Color.black;
        		}
        		else if (b % 3 == 2)
        		{
        			other.GetComponent().material.color = Color.white;
        		}
        		Debug.Log("Exit");//同等于print("")输出
        		b++;
    	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号