So I started making a small horror game last night, and I ran into a big problem. So far I have a forest, a flashlight that can turn off and on and run out of power. In the game, you have to find batteries or your flashlight turns off for good. What I would like help with is when the power in your flashlight goes out(I mean the battery dies, not the player manually turning it off), you have only a few seconds(a random number 3-10) to find a new battery or you are jumpscared by the enemy. I need help with a script for that(preferably javascript)
To recap what I need help with-
- Getting a random number 3-10
- Actually making the jump scare happened in front of you after that amount of time in seconds.
This may be a hard request, but any help is appreciated. (Scripts for battery/flashlight below)
#pragma strict
var flashlightLightSource : Light;
var lightOn : boolean = true;
var lightDrain : float = 0.1;
private static var batteryLife : float = 0.0;
var maxBatteryLife : float = 2.0;
private static var batteryPower : float = 6;
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
function Start()
{
batteryLife = maxBatteryLife;
flashlightLightSource = GetComponent(Light);
}
static function AlterEnergy (amount : int)
{
batteryLife = Mathf.Clamp(batteryLife+batteryPower, 0, 100);
}
function Update()
{
//BATTERY LIFE BRIGHTNESS//////////
if(lightOn && batteryLife >= 0)
{
batteryLife -= Time.deltaTime * lightDrain;
}
if(lightOn && batteryLife <= 0.4)
{
flashlightLightSource.GetComponent.<Light>().intensity = 5;
}
if(lightOn && batteryLife <= 0.3)
{
flashlightLightSource.GetComponent.<Light>().intensity = 4;
}
if(lightOn && batteryLife <= 0.2)
{
flashlightLightSource.GetComponent.<Light>().intensity = 3;
}
if(lightOn && batteryLife <= 0.1)
{
flashlightLightSource.GetComponent.<Light>().intensity = 2;
}
if(lightOn && batteryLife <= 0)
{
flashlightLightSource.GetComponent.<Light>().intensity = 0;
}
barDisplay = batteryLife;
if(batteryLife <= 0)
{
batteryLife = 0;
lightOn = false;
}
if(Input.GetKeyUp(KeyCode.F))
{
toggleFlashlight();
toggleFlashlightSFX();
if(lightOn)
{
lightOn = false;
}
else if (!lightOn && batteryLife >= 0)
{
lightOn = true;
}
}
}
/////// PIC ///////////
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function toggleFlashlight()
{
if(lightOn)
{
flashlightLightSource.enabled = false;
}
else
{
flashlightLightSource.enabled = true;
}
}
function toggleFlashlightSFX()
{
if(flashlightLightSource.enabled)
{
GetComponent.<AudioSource>().clip = soundTurnOn;
}
else
{
GetComponent.<AudioSource>().clip = soundTurnOff;
}
GetComponent.<AudioSource>().Play();
}
@script RequireComponent(AudioSource)
var buttonInRange;
var buttonActivated;
var batterySound : AudioClip;
private static var batteryPower : float = 10;
public var guiSkin : GUISkin;
private var hasPlayed = false;
function OnTriggerEnter (c : Collider)
{
buttonInRange = true;
}
function OnTriggerExit (c : Collider)
{
buttonInRange = false;
}
function OnGUI ()
{
if(buttonInRange == true)
{
GUI.skin = guiSkin;
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Pick up batteries");
}
}
function Update ()
{
if (buttonInRange == true)
{
if (Input.GetKeyDown ("e"))
{
if(!hasPlayed)
{
AudioSource.PlayClipAtPoint(batterySound, transform.position);
FlashLight.AlterEnergy(batteryPower);
Destroy(gameObject);
hasPlayed = true;
}
}
}
}