Guest
Public paste!

bulletTime

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