【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1522|回复: 0

4-8 Laser Grids 在场景中设置红外线检测装置

[复制链接]
发表于 2013-9-30 22:44:29 | 显示全部楼层 |阅读模式
创建激光红外线检测装置,并通过使用装置控制检测系统的开关,通过脚本触发对碰撞和灯光

LaserBlinkingC#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LaserBlinking : MonoBehaviour
  4. {
  5.     public float onTime;            // 激光开启消耗的秒数.
  6.     public float offTime;           // 激光关闭消耗的秒数.
  7.    
  8.    
  9.     private float timer;            // 激光闪烁的时间.
  10.    
  11.    
  12.     void Update ()
  13.     {
  14.         // 确切的说是时间间隔.
  15.         timer += Time.deltaTime;
  16.         
  17.         // 光束开启并且时间满足...
  18.         if(renderer.enabled && timer >= onTime)
  19.             // 光的明暗切换.
  20.             SwitchBeam();
  21.         
  22.         // 光速是关闭的并且超时...
  23.         if(!renderer.enabled && timer >= offTime)
  24.             // 光的明暗切换.
  25.             SwitchBeam();
  26.     }
  27.    
  28.    
  29.     void SwitchBeam ()
  30.     {
  31.         // 时间置0.
  32.         timer = 0f;
  33.         
  34.         // 设置明暗交替.
  35.         renderer.enabled = !renderer.enabled;
  36.         light.enabled = !light.enabled;
  37.     }
  38. }
复制代码
JS
  1. #pragma strict

  2. public var onTime : float;          // Amount of time in seconds the laser is on for.
  3. public var offTime : float;         // Amount of time in seconds the laser is off for.


  4. private var timer : float;          // Timer to time the laser blinking.


  5. function Update ()
  6. {
  7.     // Increment the timer by the amount of time since the last frame.
  8.     timer += Time.deltaTime;
  9.    
  10.     // If the beam is on and the onTime has been reached...
  11.     if(renderer.enabled && timer >= onTime)
  12.         // Switch the beam.
  13.         SwitchBeam();
  14.    
  15.     // If the beam is off and the offTime has been reached...
  16.     if(!renderer.enabled && timer >= offTime)
  17.         // Switch the beam.
  18.         SwitchBeam();
  19. }


  20. function SwitchBeam ()
  21. {
  22.     // Reset the timer.
  23.     timer = 0f;
  24.    
  25.     // Switch whether the beam and light are on or off.
  26.     renderer.enabled = !renderer.enabled;
  27.     light.enabled = !light.enabled;
  28. }
复制代码
LaserPlayerDetection
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LaserPlayerDetection : MonoBehaviour
  4. {
  5.     private GameObject player;                          // 指向玩家的变量.
  6.     private LastPlayerSighting lastPlayerSighting;      // 指向玩家的视野的全局变量.


  7.     void Awake ()
  8.     {
  9.         // 设置变量.
  10.         player = GameObject.FindGameObjectWithTag(Tags.player);
  11.         lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<LastPlayerSighting>();
  12.     }


  13.     void OnTriggerStay(Collider other)
  14.     {
  15.         // 如果激光在正常渲染...
  16.         if(renderer.enabled)
  17.             // ... 如果进入碰撞区域的是玩家...
  18.             if(other.gameObject == player)
  19.                 // ... 将聚焦点设在玩家身上.
  20.                 lastPlayerSighting.position = other.transform.position;
  21.     }
  22. }
复制代码
JS
  1. #pragma strict

  2. private var player : GameObject;                                // Reference to the player.
  3. private var lastPlayerSighting : LastPlayerSighting;        // Reference to the global last sighting of the player.


  4. function Awake ()
  5. {
  6.     // Setting up references.
  7.     player = GameObject.FindGameObjectWithTag(Tags.player);
  8.     lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
  9. }


  10. function OnTriggerStay(other : Collider)
  11. {
  12.     // If the beam is on...
  13.     if(renderer.enabled)
  14.         // ... and if the colliding gameobject is the player...
  15.         if(other.gameObject == player)
  16.             // ... set the last global sighting of the player to the colliding object's position.
  17.             lastPlayerSighting.position = other.transform.position;
  18. }
复制代码
LaserSwitchDeactivationC#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LaserSwitchDeactivation : MonoBehaviour
  4. {
  5.         public GameObject laser;                // 指向能被我们关闭的激光束.
  6.         public Material unlockedMat;            // 未锁定的材质.

  7.         private GameObject player;              // 指向玩家.


  8.         void Awake ()
  9.         {
  10.                 // 设置变量.
  11.                 player = GameObject.FindGameObjectWithTag(Tags.player);
  12.         }


  13.         void OnTriggerStay (Collider other)
  14.         {
  15.                 // 如果进入区域的是玩家...
  16.                 if(other.gameObject == player)
  17.                         // ... 并且按下了Switch按钮...
  18.                         if(Input.GetButton("Switch"))
  19.                                 // ... 执行关闭激光束函数.
  20.                                 LaserDeactivation();
  21.         }


  22.         void LaserDeactivation ()
  23.         {
  24.                 // 关闭激光束.
  25.                 laser.SetActive(false);

  26.                 // 获取关闭装置中显示器的renderer组件  // Store the renderer component of the screen.
  27.                 Renderer screen = transform.Find("prop_switchUnit_screen_001").renderer;

  28.                 // 将材质修改为unlocked  // Change the material of the screen to the unlocked material.
  29.                 screen.material = unlockedMat;

  30.                 // 播放关闭声音 Play switch deactivation audio clip.
  31.                 audio.Play();
  32.         }
  33. }
复制代码
js
  1. #pragma strict

  2. public var laser : GameObject;              // Reference to the laser that can we turned off at this switch.
  3. public var unlockedMat : Material;          // The screen's material to show the laser has been unloacked.

  4. private var player : GameObject;            // Reference to the player.


  5. function Awake ()
  6. {
  7.     // Setting up the reference.
  8.     player = GameObject.FindGameObjectWithTag(Tags.player);
  9. }


  10. function OnTriggerStay (other : Collider)
  11. {
  12.     // If the colliding gameobject is the player...
  13.     if(other.gameObject == player)
  14.         // ... and the switch button is pressed...
  15.         if(Input.GetButton("Switch"))
  16.             // ... deactivate the laser.
  17.             LaserDeactivation();
  18. }


  19. function LaserDeactivation ()
  20. {
  21.     // Deactivate the laser GameObject.
  22.     laser.SetActive(false);
  23.    
  24.     // Store the renderer component of the screen.
  25.     var screen : Renderer = transform.Find("prop_switchUnit_screen_001").renderer;
  26.    
  27.     // Change the material of the screen to the unlocked material.
  28.     screen.material = unlockedMat;
  29.    
  30.     // Play switch deactivation audio clip.
  31.     audio.Play();
  32. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 19:18

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表