unity官方demo学习之Stealth(七)激光门

来源:互联网 发布:赛尔网络创新大赛 编辑:程序博客网 时间:2024/04/29 14:06
七,激光门

1,将models中的fx_laserFence_lasers拖到层级视图中,设置如图009,并且方便管理编号_001


2,为其添加box collider组件,勾选 is trigger
3,为其添加音频组件,将laser_hum拖进去,勾选loop,3d中的Min Distance设为1.8
4,添加light组件,设置颜色(255,40,0),range改成5,intensity改为0.6,lightmapping设为 realtime only(这样当门关上时不会
重新渲染此light使光还留在场景中)

5,添加脚本DoneLaserBlinking 控制激光门开关

using UnityEngine;using System.Collections;public class DoneLaserBlinking : MonoBehaviour{public float onTime;// Amount of time in seconds the laser is on for.public float offTime;// Amount of time in seconds the laser is off for.private float timer;// Timer to time the laser blinking.void Update (){// Increment the timer by the amount of time since the last frame.        timer += Time.deltaTime;// If the beam is on and the onTime has been reached...        if(renderer.enabled && timer >= onTime)// Switch the beam.            SwitchBeam();// If the beam is off and the offTime has been reached...        if(!renderer.enabled && timer >= offTime)// Switch the beam.            SwitchBeam();}void SwitchBeam (){// Reset the timer.timer = 0f;// Switch whether the beam and light are on or off.    renderer.enabled = !renderer.enabled;light.enabled = !light.enabled;}}

变量:门开启时间长度
      门关闭时间长度
      门间隔时间计时器


函数:update
使计时器开始工作(自增)
若渲染开启且超过开启时间
调用SwitchBeam()使激光门关闭(使计时器归零;变换渲染和光照状态)
若渲染没开启且超过关闭时间
调用SwitchBeam()是激光门开启(使计时器归零;变换渲染和光照状态)


SwitchBeam

使计时器归零;变换渲染和光照状态是与之前相反




6,添加脚本DoneLaserPlayerDetection 检测激光门与玩家的碰撞


using UnityEngine;using System.Collections;public class DoneLaserPlayerDetection : MonoBehaviour{    private GameObject player;// Reference to the player.    private DoneLastPlayerSighting lastPlayerSighting;// Reference to the global last sighting of the player.    void Awake ()    {// Setting up references.player = GameObject.FindGameObjectWithTag(DoneTags.player);lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneLastPlayerSighting>();    }    void OnTriggerStay(Collider other)    {// If the beam is on...        if(renderer.enabled)// ... and if the colliding gameobject is the player...            if(other.gameObject == player)// ... set the last global sighting of the player to the colliding object's position.                lastPlayerSighting.position = other.transform.position;    }}

变量:玩家对象引用(私有)
      最后玩家坐标引用


函数:Awake()
设置玩家对象和玩家最后地址的脚本


      OnTriggerStay()碰撞时调用
判断渲染是否开启
判断是否碰撞的是玩家对象
如果是,更新玩家最后的位置


7,复制得到另外5个激光门
002(-8,1.21,9.23),003(-17.93,1.21,24.08), 004(-23.92,1.21,26.1)Y角度由90改为0 , 005(-8.95,1.21,25.99) , 006(-8.95,1.21,29.96)
8,设置5,6号门onTime时间为1.5, 5号offTime为1.5, 6号门offTime为1.55

9,将prop_switchUnit模型拖入到010图所示房间,(-12.5,0,11.1),角度Y为180


10,(这个开关以后会被烘焙)所以勾选static,添加组件box collider(防止玩家穿过开关),调小框center(0.3,0.8,-0.15),size(1.3,1.6,0.81)

11,设置开关操作范围,添加组件sphere collider,center(0,1,1),radius1.5,勾选is Trigger

12,添加脚本DoneLaserSwitchDeactivation

using UnityEngine;using System.Collections;public class DoneLaserSwitchDeactivation : MonoBehaviour{public GameObject laser;// Reference to the laser that can we turned off at this switch.public Material unlockedMat; // The screen's material to show the laser has been unloacked.private GameObject player;// Reference to the player.void Awake (){// Setting up the reference.player = GameObject.FindGameObjectWithTag(DoneTags.player);}void OnTriggerStay (Collider other){// If the colliding gameobject is the player...if(other.gameObject == player)// ... and the switch button is pressed...if(Input.GetButton("Switch"))// ... deactivate the laser.LaserDeactivation();}void LaserDeactivation (){// Deactivate the laser GameObject.laser.SetActive(false);// Store the renderer component of the screen.Renderer screen = transform.Find("prop_switchUnit_screen_001").renderer;// Change the material of the screen to the unlocked material.screen.material = unlockedMat;// Play switch deactivation audio clip.audio.Play();}}

变量:激光门引用
      屏幕材质(提示激光门已解锁)
      玩家对象引用


函数:Awake()
获得玩家引用


     OnTriggerStay ()检测玩家是否进入操作范围
判断是否是玩家
判断玩家是否按下开关
调用关激光门函数LaserDeactivation()
     
     LaserDeactivation()
禁用激光门
屏幕是开关的子元素,通过寻找子元素找到屏幕,再访问render渲染组件
将屏幕的材质换成unlockedMat(后面会回到unity中设置)
播放操作成功的音效


13,回到unity,设置Unlocked Mat为prop_switchUnit_screen_unlocked,将对应的子对象改为prop_switchUnit_screen_001
14,将prop_switchUnit保存到prefab
15,为开关分配对应控制的门(再复制3个开关出来,对4个开关编号,第二个位置-1.6,0,11.3;3:-17.7,0,33.3;4:-30,0,33,3)。将001到004门拖到对应开关脚本文件中的Laser栏中
16,脚本管理(因为都已经设置好了,如果你们的脚本都是后来创建的就看视频分包管理吧)
17,烘焙高质量的灯光贴图,windows->lightmapping.右边点击Bake,quality选high,pading选3,最后点击Bake Scene
等待成果吧。。。



0 0
原创粉丝点击