bulletTime
By: a guest | Mar 20th, 2010 | Syntax:
JavaScript | Size: 1.73 KB | Hits: 74 | Expires: Never
// B I G thanks to ForestJohson on the Unity IRC.
var crosshairTexture : Texture2D;
var weaponSelect : Texture2D;
var position : Rect;
var position_Select : Rect;
var bulletTime = false;
var targetTimeScale = 1.0;
var targetAudioScale = 1.0;
var fadeSpeed : float = 1;
function Start ()
{
position = Rect( ( Screen.width - crosshairTexture.width ) / 2, ( Screen.height - crosshairTexture.height ) / 2, crosshairTexture.width, crosshairTexture.height );
position_Select = Rect( ( Screen.width - weaponSelect.width ) / 2, ( Screen.height - weaponSelect.height ) / 2, weaponSelect.width, weaponSelect.height );
}
function OnGUI() // Switch crosshair Texture based on bulletTime
{
if (bulletTime)
{
GUI.DrawTexture( position_Select, weaponSelect );
}
else
{
GUI.DrawTexture( position, crosshairTexture );
}
}
function Update ()
{
Time.timeScale = Mathf.Lerp (Time.timeScale, targetTimeScale, Time.deltaTime * fadeSpeed);
audio.pitch = Mathf.Lerp (Time.timeScale, targetAudioScale, Time.deltaTime * fadeSpeed);
if (Input.GetButtonDown ("Fire2")) // toggle bullet time on button down
{
if(bulletTime)
{
// bulletTime off.
Time.timeScale = 1.0;
audio.pitch = 1;
RenderSettings.ambientLight = new Color (0.2, 0.2, 0.2);
}
else
{
// bulletTime on.
Time.timeScale = 0.08;
audio.pitch = 0.6;
RenderSettings.ambientLight = new Color (0.01, 0.01, 0.01);
}
// Adjust fixed delta time according to timescale
// The fixed delta time will now be 0.02 frames per real-time second
Time.fixedDeltaTime = 0.02 * Time.timeScale;
// maintain the status of the bulleTime var, actually toggle it and stuff
bulletTime = !bulletTime;
}
}