Share Pastebin
Guest
Public paste!

bulletTime

By: a guest | Mar 21st, 2010 | Syntax: JavaScript | Size: 2.27 KB | Hits: 34 | 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 = 0.5;
  11. var fadeSpeed = 1.0;
  12. var playerSpeed = 1.0;
  13. var normalAudio:GameObject;
  14. var bulletTimeAudio:GameObject;
  15.  
  16.  
  17. function Start ()
  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.  
  22.         normalAudio = GameObject.Find("normalAudio");
  23.         mainAudio = normalAudio.GetComponent("AudioSource");
  24.        
  25.         bulletTimeAudio = GameObject.Find("bulletTimeAudio");
  26.         timeAudio = bulletTimeAudio.GetComponent("AudioSource");
  27. }
  28.  
  29. function OnGUI() // Switch crosshair Texture based on bulletTime
  30. {
  31.         if (bulletTime)
  32.         {
  33.                 GUI.DrawTexture( position_Select, weaponSelect );      
  34.         }
  35.         else
  36.         {
  37.                 GUI.DrawTexture( position, crosshairTexture );
  38.         }
  39. }
  40.  
  41.  
  42. function Update ()
  43. {
  44.         // Adjust fixed delta time according to timescale
  45.         // The fixed delta time will now be 0.02 frames per real-time second
  46.            Time.fixedDeltaTime = 0.02 * Time.timeScale;
  47.        
  48.  
  49.         normalAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  50.         bulletTimeAudio.audio.pitch = Mathf.Lerp (Time.timeScale, targetPitch, Time.deltaTime * fadeSpeed);
  51.         Time.timeScale = Mathf.Lerp (Time.timeScale, targetTimeScale, Time.deltaTime * fadeSpeed);
  52.         renderSettings = Mathf.Lerp (Time.timeScale, targetTimeScale, Time.deltaTime * fadeSpeed);
  53.  
  54.  
  55.     if (Input.GetButtonDown ("Fire2")) // toggle bullet time on button down
  56.         {      
  57.                 if(bulletTime)
  58.                 {
  59.                         // bulletTime off.
  60.                         targetTimeScale = 1.0;
  61.                         targetPitch = 1;
  62.                         RenderSettings.ambientLight = new Color (0.2, 0.2, 0.2);
  63.                         playerSpeed = 2.0;
  64.                 }
  65.                 else
  66.                 {
  67.                         // bulletTime on.
  68.                         playerSpeed = 5000.0;
  69.                         targetTimeScale = 0.08;
  70.                         targetPitch = 0.9;
  71.                         RenderSettings.ambientLight = new Color (0.01, 0.01, 0.01);
  72.                        
  73.                 }
  74.                
  75.  
  76.                
  77.                 // maintain the status of the bulleTime var, actually toggle it and stuff
  78.                 bulletTime = !bulletTime;
  79.         }
  80. }