Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

DaBossTMR

Flashlight Script v2.1.1

Recommended Posts

I mean, you could check to see if the light is off, and if it is you could add to the battery a certain amount and wait a certain amount of seconds in between adding more juice.

Share this post


Link to post
Share on other sites

There is a "on" boolean, you must check if it's equal to false (already in the script) and then if it is off, then add Time.deltaTime * (rate of recharge) to the battery percentage float value.

Share this post


Link to post
Share on other sites

Nice effect. Thx ++

Share this post


Link to post
Share on other sites

i get this error :((

 

NullReferenceException: Object reference not set to an instance of an object
Flashlight.Update () (at Assets/scripts/Flash/Flashlight.cs:24)
 

sory for writing in old post please dont hit me  I-)

Share this post


Link to post
Share on other sites

crap now i feel stupid, to muth work to day :P
 

but now i get this error in the HUD Script

 

Assets/scripts/Flash/HUD.cs(15,72): error CS0117: `Flashlight' does not contain a definition for `BatteryLife'

Share this post


Link to post
Share on other sites

batteryPercentage, not BatteryLife. Do you even know how to read a script?

 

Okay, to clarify any future problems...

batteryPercentage goes from 0 to 100.

Share this post


Link to post
Share on other sites
Please login or register to see this quote.

No the script doesn't call for that, you would have implement that yourself, or make a battery object + script and have the battery able to recharge the flashlight.

Share this post


Link to post
Share on other sites

Here is a UnityScript Version of This Flashlight Script.

 

Includes :

Battery Recharge When The Flashlight Is Off.

Simple GUI Label That Tell You The % Of The Charge Rate.

 

Here is the Script. :)

Please login or register to see this spoiler. Edited by Epic-Shot
  • Like 1

Share this post


Link to post
Share on other sites

Please login or register to see this code.

Made a quick JavaScript version of this script, not tested either.

var clickSound : AudioClip;

var batteryLifeInSec : float = 300;

var batteryPercentage : float = 100;

private var on : boolean;

private var timer : float;

function Update () {

var lite = GetComponent.<Light>();

timer += Time.deltaTime;

if(Input.GetKeyDown(KeyCode.F) && timer >= 0.3f && batteryPercentage > 0) {

on = !on;

audio.PlayOneShot(clickSound);

timer = 0;

}

if(on) {

lite.enabled = true;

batteryPercentage -= Time.deltaTime * (100 / batteryLifeInSec);

}

else {

lite.enabled = false;

}

batteryPercentage = Mathf.Clamp(batteryPercentage, 0, 100);

if(batteryPercentage == 0) {

lite.intensity = Mathf.Lerp(lite.intensity, 0, Time.deltaTime * 2);

}

if(batteryPercentage > 0 && batteryPercentage < 25) {

lite.intensity = Mathf.Lerp(lite.intensity, 0.5f, Time.deltaTime);

}

if(batteryPercentage > 25 && batteryPercentage < 75) {

lite.intensity = Mathf.Lerp(lite.intensity, 0.8f, Time.deltaTime);

}

if(batteryPercentage > 75 && batteryPercentage <= 100) {

lite.intensity = Mathf.Lerp(lite.intensity, 1, Time.deltaTime);

}

}

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...