Flashlight script wont follow firstperson
I am hoping someone can help me with this. I am attached a spotlight (flashlight) to my first person controller as a child and placed a flashlight script. Although, my first perosn controller does not have a main camera just a first person
I adjusted the light so that it was working perfectly, but for some reason the light has become idle and will not move. I have tried doing this process multiple times and after a while the flashlight just becomes idle again
Here is the script
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 = 1;
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.().intensity = 5; } if(lightOn && batteryLife <= 0.3) { flashlightLightSource.GetComponent.().intensity = 4; } if(lightOn && batteryLife <= 0.2) { flashlightLightSource.GetComponent.().intensity = 3; } if(lightOn && batteryLife <= 0.1) { flashlightLightSource.GetComponent.().intensity = 2; } if(lightOn && batteryLife <= 0) { flashlightLightSource.GetComponent.().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.().clip = soundTurnOn; } else { GetComponent.().clip = soundTurnOff; } GetComponent.().Play();
}
@script RequireComponent(AudioSource)
Your answer
Follow this Question
Related Questions
How Do I Make A Realistic Flashlight? 2 Answers
Spotlight Flashlight Cut Off When Near Object? 0 Answers
Spotlight through objects - HELP 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How to make brake car ? 2 Answers