Guest
Public paste!

bulletTime

By: a guest | Mar 20th, 2010 | Syntax: JavaScript | Size: 1.73 KB | Hits: 74 | Expires: Never
Copy text to clipboard
  1. // B I G thanks to ForestJohson 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 = 1.0;
  10. var targetAudioScale = 1.0;
  11. var fadeSpeed : float = 1;
  12.  
  13.  
  14. function Start ()
  15. {
  16.         position = Rect( ( Screen.width - crosshairTexture.width ) / 2, ( Screen.height - crosshairTexture.height ) / 2, crosshairTexture.width, crosshairTexture.height );
  17.         position_Select = Rect( ( Screen.width - weaponSelect.width ) / 2, ( Screen.height - weaponSelect.height ) / 2, weaponSelect.width, weaponSelect.height );
  18. }
  19.  
  20. function OnGUI() // Switch crosshair Texture based on bulletTime
  21. {
  22.  
  23.         if (bulletTime)
  24.         {
  25.                 GUI.DrawTexture( position_Select, weaponSelect );      
  26.         }
  27.         else
  28.         {
  29.                 GUI.DrawTexture( position, crosshairTexture );
  30.         }
  31. }
  32.  
  33.  
  34. function Update ()
  35. {
  36.        
  37.         Time.timeScale = Mathf.Lerp (Time.timeScale, targetTimeScale, Time.deltaTime * fadeSpeed);
  38.         audio.pitch = Mathf.Lerp (Time.timeScale, targetAudioScale, Time.deltaTime * fadeSpeed);
  39.  
  40.  
  41.  
  42.     if (Input.GetButtonDown ("Fire2")) // toggle bullet time on button down
  43.         {      
  44.                 if(bulletTime)
  45.                 {
  46.                         // bulletTime off.
  47.                         Time.timeScale = 1.0;
  48.                         audio.pitch = 1;
  49.                         RenderSettings.ambientLight = new Color (0.2, 0.2, 0.2);
  50.                 }
  51.                 else
  52.                 {
  53.                         // bulletTime on.
  54.                         Time.timeScale = 0.08;
  55.                         audio.pitch = 0.6;
  56.                         RenderSettings.ambientLight = new Color (0.01, 0.01, 0.01);
  57.                        
  58.                 }
  59.                
  60.                 // Adjust fixed delta time according to timescale
  61.                 // The fixed delta time will now be 0.02 frames per real-time second
  62.                 Time.fixedDeltaTime = 0.02 * Time.timeScale;
  63.                
  64.                 // maintain the status of the bulleTime var, actually toggle it and stuff
  65.                 bulletTime = !bulletTime;
  66.         }
  67. }