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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

查看: 1819|回复: 0

4-5 Game Controller 游戏控制器

[复制链接]
发表于 2013-9-30 22:32:41 | 显示全部楼层 |阅读模式
创建游戏控制器,使之能很好地为游戏中诸多的操作服务,比如控制警报系统的警报灯
C#脚本
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LastPlayerSighting : MonoBehaviour
  4. {
  5.     public Vector3 position = new Vector3(1000f, 1000f, 1000f);       // 玩家没有被发现时候的视野初始坐标.
  6.     public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);  // 为判断是否安全预设的一个坐标.
  7.     public float lightHighIntensity = 0.25f;                         // 警报灯没响时方向光的强度最大值.
  8.     public float lightLowIntensity = 0f;                              // 警报灯响起的时候方向光的最小值
  9.     public float fadeSpeed = 7f;                                        // 光从低到高亮渐隐的速度
  10.     public float musicFadeSpeed = 1f;                                 // 音乐渐隐的速度
  11.         
  12.     private AlarmLight alarm;                                          // 预设一个AlarmLight脚本变量.
  13.     private Light mainLight;                                            // 预设一个main light 的light变量.
  14.     private AudioSource panicAudio;                                   // 预设一个AudioSources脚本变量紧张音乐.
  15.     private AudioSource[] sirens;                                       // 预设一组sirens 声音变量.
  16.         
  17.     void Awake ()
  18.     {
  19.         // 设置alarm的引用通过Tag管理器找到Light_directional_alarm中的AlarmLight脚本.
  20.         alarm = GameObject.FindGameObjectWithTag(Tags.alarmLight).GetComponent<AlarmLight>();
  21.         
  22.         // 设置mainLight的引用通过Tag管理器找到Light_directional_main中的light .
  23.         mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
  24.         
  25.         // 设置panicAudio的引用通过Tag管理器找到secondaryMusic中的audio.
  26.         panicAudio = transform.Find("secondaryMusic").audio;
  27.         
  28.         // 获取返回多个大喇叭的数组.
  29.         GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);
  30.         
  31.         // 创建一个跟sirenGameObjects数组个数一样的一个新数组.
  32.         sirens = new AudioSource[sirenGameObjects.Length];
  33.         
  34.         // 把六个大喇叭的组件中的声音存在新数组中.
  35.         for(int i = 0; i < sirens.Length; i++)
  36.         {
  37.             sirens[i] = sirenGameObjects[i].audio;
  38.         }
  39.     }   
  40.    
  41.     void Update ()
  42.     {
  43.         // 更替背景音乐.
  44.         SwitchAlarms();
  45.               MusicFading();
  46.     }
  47.    
  48.     void SwitchAlarms ()
  49.     {
  50.         // 判断警报是否该响起.
  51.         alarm.alarmOn = position != resetPosition;
  52.         //灯光强度去向变量
  53.         float newIntensity;
  54.         
  55.         // 如果position与resetPosition不相符,即警报响起的时候...
  56.         if(position != resetPosition)
  57.             // ... 设置将要显示的光强度为弱光.
  58.             newIntensity = lightLowIntensity;
  59.         else
  60.             // 设置当将要显示的强度为强光.
  61.             newIntensity = lightHighIntensity;
  62.         
  63.         // 给场景中的主光源从当前光强做将要光强的插值.
  64.         mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
  65.         
  66.         // 循环遍历大喇叭
  67.         for(int i = 0; i < sirens.Length; i++)
  68.         {
  69.             // ... 如果警报被激活了,警报铃大喇叭还没响起的话就响
  70.             if(position != resetPosition && !sirens[i].isPlaying)
  71.                 sirens[i].Play();
  72.             // 如果警报没被激活,警报铃大喇叭就不出声.
  73.             else if(position == resetPosition)
  74.                 sirens[i].Stop();
  75.         }
  76.     }   
  77.    
  78.     void MusicFading ()
  79.     {
  80.         // 如果警报被激活...
  81.         if(position != resetPosition)
  82.         {
  83.             // ... 普通背景音乐淡出...
  84.             audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
  85.             
  86.             // ... 紧张节奏的音乐渐渐奏起.
  87.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
  88.         }
  89.         else
  90.         {
  91.             // 紧张背景音乐淡出,普通背景音乐渐响
  92.             audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
  93.             panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
  94.         }
  95.     }
  96. }
复制代码
JS脚本
  1. #pragma strict

  2. public var position : Vector3 = new Vector3(1000f, 1000f, 1000f);       // The last global sighting of the player.
  3. public var resetPosition : Vector3 = new Vector3(1000f, 1000f, 1000f);  // The default position if the player is not in sight.
  4. public var lightHighIntensity : float = 0.25f;                          // The directional light's intensity when the alarms are off.
  5. public var lightLowIntensity : float = 0f;                              // The directional light's intensity when the alarms are on.
  6. public var fadeSpeed : float = 7f;                                      // How fast the light fades between low and high intensity.
  7. public var musicFadeSpeed : float = 1f;                                 // The speed at which the


  8. private var alarm : AlarmLight;                                     // Reference to the AlarmLight script.
  9. private var mainLight : Light;                                          // Reference to the main light.
  10. private var panicAudio : AudioSource;                                       // Reference to the AudioSource of the panic msuic.
  11. private var sirens : AudioSource[];                                     // Reference to the AudioSources of the megaphones.


  12. function Awake ()
  13. {
  14.     // Setup the reference to the alarm light.
  15.     alarm = GameObject.FindGameObjectWithTag(Tags.alarm).GetComponent(AlarmLight);
  16.    
  17.     // Setup the reference to the main directional light in the scene.
  18.     mainLight = GameObject.FindGameObjectWithTag(Tags.mainLight).light;
  19.    
  20.     // Setup the reference to the additonal audio source.
  21.     panicAudio = transform.Find("secondaryMusic").audio;
  22.    
  23.     // Find an array of the siren gameobjects.
  24.     var sirenGameObjects : GameObject[] = GameObject.FindGameObjectsWithTag(Tags.siren);
  25.    
  26.     // Set the sirens array to have the same number of elements as there are gameobjects.
  27.     sirens = new AudioSource[sirenGameObjects.Length];
  28.    
  29.     // For all the sirens allocate the audio source of the gameobjects.
  30.     for(var i = 0; i < sirens.Length; i++)
  31.     {
  32.         sirens[i] = sirenGameObjects[i].audio;
  33.     }
  34. }


  35. function Update ()
  36. {
  37.     // Switch the alarms and fade the music.
  38.     SwitchAlarms();
  39.     MusicFading();
  40. }


  41. function SwitchAlarms ()
  42. {
  43.     // Set the alarm light to be on or off.
  44.     alarm.alarmOn = position != resetPosition;
  45.    
  46.     // Create a new intensity.
  47.     var newIntensity : float;
  48.    
  49.     // If the position is not the reset position...
  50.     if(position != resetPosition)
  51.         // ... then set the new intensity to low.
  52.         newIntensity = lightLowIntensity;
  53.     else
  54.         // Otherwise set the new intensity to high.
  55.         newIntensity = lightHighIntensity;
  56.    
  57.     // Fade the directional light's intensity in or out.
  58.     mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
  59.    
  60.     // For all of the sirens...
  61.     for(var i = 0; i < sirens.Length; i++)
  62.     {
  63.         // ... if alarm is triggered and the audio isn't playing, then play the audio.
  64.         if(position != resetPosition && !sirens[i].isPlaying)
  65.             sirens[i].Play();
  66.         // Otherwise if the alarm isn't triggered, stop the audio.
  67.         else if(position == resetPosition)
  68.             sirens[i].Stop();
  69.     }
  70. }


  71. function MusicFading ()
  72. {
  73.     // If the alarm is not being triggered...
  74.     if(position != resetPosition)
  75.     {
  76.         // ... fade out the normal music...
  77.         audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
  78.         
  79.         // ... and fade in the panic music.
  80.         panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
  81.     }
  82.     else
  83.     {
  84.         // Otherwise fade in the normal music and fade out the panic music.
  85.         audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
  86.         panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
  87.     }
  88. }
复制代码

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

GMT+8, 2024-9-28 00:22

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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