Share Pastebin
Guest
Public paste!

bulletTime

By: a guest | Mar 21st, 2010 | Syntax: JavaScript | Size: 2.60 KB | Hits: 135 | Expires: Never
Copy text to clipboard
  1. // B I G thanks to ForestJohsson on the Unity IRC.
  2.  
  3.  
  4. var crosshairTexture : Texture2D;
  5. var weaponSelect : Texture2D;
  6. var position : Rect;
  7. var position_Select : Rect;
  8. var bulletTime = false;
  9. var targetTimeScale = 2.0;
  10. var targetPitch = 1.0;
  11. var fadeSpeed = 4.0;
  12. var playerSpeed = 1.0;
  13. var normalAudio:GameObject;
  14. var bulletTimeAudio:GameObject;
  15.  
  16. function Start ()
  17. {
  18.  
  19.         position = Rect( ( Screen.width - crosshairTexture.width ) / 2, ( Screen.height - crosshairTexture.height ) / 2, crosshairTexture.width, crosshairTexture.height );
  20.         position_Select = Rect( ( Screen.width - weaponSelect.width ) / 2, ( Screen.height - weaponSelect.height ) / 2, weaponSelect.width, weaponSelect.height );
  21.         normalAudio = GameObject.Find("normalAudio");
  22.         mainAudio = normalAudio.GetComponent("AudioSource");
  23.         bulletTimeAudio = GameObject.Find("bulletTimeAudio");
  24.         timeAudio = bulletTimeAudio.GetComponent("AudioSource");
  25.        
  26.         bulletTimeAudio.audio.pitch = targetPitch;
  27.         normalAudio.audio.pitch = targetPitch;
  28. }
  29.  
  30. function OnGUI() // Switch crosshair Texture based on bulletTime
  31. {
  32.         if (bulletTime)
  33.         {
  34.                 GUI.DrawTexture( position_Select, weaponSelect );      
  35.         }
  36.         else
  37.         {
  38.                 GUI.DrawTexture( position, crosshairTexture );
  39.         }
  40. }
  41.  
  42.  
  43. function Update ()
  44. {
  45.         // Adjust fixed delta time according to timescale
  46.         // The fixed delta time will now be 0.02 frames per real-time second
  47.         Time.fixedDeltaTime = 0.02 * Time.timeScale;
  48.  
  49.         Time.timeScale = Mathf.Lerp (Time.timeScale, targetTimeScale, Time.deltaTime * fadeSpeed);
  50.  
  51.     if (Input.GetButtonDown ("Fire2")) // toggle bullet time on button down
  52.         {      
  53.                 if(bulletTime)
  54.                 {
  55.                         // bulletTime off.
  56.                         targetTimeScale = 1.0;
  57.                         targetPitch = 1.0;
  58.                         RenderSettings.ambientLight = Color.Lerp (Color (0.2, 0.2, 0.2), Color (0.02, 0.02, 0.02), 0.03);
  59.                         playerSpeed = 1.0;
  60.                         bulletTimeAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  61.                         normalAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  62.                 }
  63.                 else
  64.                 {
  65.                         // bulletTime on.
  66.                         playerSpeed = 15000.0;
  67.                         RenderSettings.ambientLight = Color.Lerp (Color (0.02, 0.02, 0.02), Color (0.2, 0.2, 0.2), 0.03);
  68.                         targetTimeScale = 0.08;
  69.                         targetPitch = 0.8;
  70.                         bulletTimeAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  71.                         normalAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  72.                 //      RenderSettings.ambientLight = new Color (0.01, 0.01, 0.01);
  73.                        
  74.                 }
  75.                
  76.  
  77.                
  78.                 // maintain the status of the bulleTime var, actually toggle it and stuff
  79.                 bulletTime = !bulletTime;
  80.         }
  81. }