Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cherikov · Aug 20, 2012 at 11:55 AM · lightcomponenteffectflashlight

Flashlight Flicker!

hey guys, I need some help with a first Person Survival/Adventure game that involves you searching the forest for survivors of a plane crash. After about 5 minutes in game, I want the Flashlight to begin to flicker. Here is my flashlight script:

 var flashlightOn : boolean = false;
 
 
 
 var soundOn : AudioClip;
 
 
 
 var soundOff : AudioClip;
 
 
 
 var onIntensity = 5.0;
 
 
 
 var offIntensity = 0.0;
 
 
 
  
 
 
 
 function Update () {
 
 
 
    //Checks if the F key is down and whether the boolean is on or off.
 
 
 
    if(Input.GetKeyDown(KeyCode.F) && flashlightOn == false){
 
 
 
    
 
 
 
    AudioSource.PlayClipAtPoint(soundOn, light.transform.position);
 
 
 
    flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.
 
 
 
    light.intensity = onIntensity;
 
 
 
    
 
 
 
    } else {
 
 
 
    
 
 
 
    if(Input.GetKeyDown(KeyCode.F) && flashlightOn == true) {
 
 
 
    
 
 
 
    AudioSource.PlayClipAtPoint(soundOff, light.transform.position);
 
 
 
    flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.
 
 
 
    light.intensity = offIntensity;
 
 
 
       }
 
 
 
    }
 
 
 
    
 
 
 
 }


And This is my flicker effect:

// Properties

var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)

var base : float = 0.0; // start

var amplitude : float = 1.0; // amplitude of the wave

var phase : float = 0.0; // start point inside on wave cycle

var frequency : float = 0.5; // cycle frequency per second

// Keep a copy of the original color

private var originalColor : Color;

// Store the original color

function Start () {

 originalColor = GetComponent(Light).color;



}

function Update () {

var light : Light = GetComponent(Light);

light.color = originalColor * (EvalWave());

}

function EvalWave () {

var x : float = (Time.time + phase)*frequency;

var y : float;

x = x - Mathf.Floor(x); // normalized value (0..1)

if (waveFunction=="sin") {

 y = Mathf.Sin(x*2*Mathf.PI);



}

else if (waveFunction=="tri") {

 if (x < 0.5) 



   y = 4.0 * x - 1.0;



 else



   y = -4.0 * x + 3.0;  



}

else if (waveFunction=="sqr") {

 if (x < 0.5) 



   y = 1.0;



 else



   y = -1.0;  



}

else if (waveFunction=="saw") {

   y = x;



}

else if (waveFunction=="inv") {

 y = 1.0 - x;



}

else if (waveFunction=="noise") {

 y = 1 - (Random.value*2);



}

else {

 y = 1.0;



}

return (y*amplitude)+base;

}

How would I make it so that after about 5 minutes, the 2 script is activated and then about 10 seconds after that, both scripts are deactivated - emulating a fading battery? your help would be greatly appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Itinerant · Aug 20, 2012 at 04:15 PM

I won't copy it over, but if you go check out this tutorial and scroll down to 'Countdown Timer,' you could make one and set triggers at the 5:00 and 5:10 marks that toggle booleans to activate/deactivate the scripts.

http://active.tutsplus.com/tutorials/unity/getting-started-with-unity-gui-scoring-timers-particles/

Hope that helps!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Itinerant · Aug 21, 2012 at 09:30 PM 0
Share

Ok, here's some clarification :) I haven't customized the variables to your script, but it should be pretty easy to see what you'd need to edit to use it.

private var flashFlicker : boolean = false; private var flashOn : boolean = true;

private var timerOn : boolean = true;

function timerRun(){ while(timerOn == true){ yield WaitForSeconds(300.0); flashFlicker = true; yield WaitForSeconds(10.0); flashOn = false; flashFlicker = false; timerOn = false; } } timerRun();

avatar image Cherikov · Aug 22, 2012 at 01:52 AM 0
Share

ok so i would put this on my flashlight right as a seperate script and rename the variables to the 2 script names correct??

avatar image Itinerant · Aug 22, 2012 at 02:24 AM 0
Share

That should do the job. Oh, or you could toss out those variables and place a this.GetComponent(ScriptName).enabled=false; in their place. Bit more compact, now that I'm thinking about it.

avatar image Itinerant · Aug 22, 2012 at 02:29 AM 0
Share

Oh, or even shorter:

function timerRun(){
		yield WaitForSeconds(300.0);
			this.GetComponent(FlashlightFlickerScript).enabled=true;
		yield WaitForSeconds(10.0);
			this.GetComponent(FlashlightFlickerScript).enabled=false;
			this.GetComponent(FlashlightOnScript).enabled=false;
	}
	
timerRun();

As you can tell by now, I'm not the most efficient scripter (yet) either :D But I'm testing these before giving them to you, so they should all work :)

avatar image
0

Answer by Cherikov · Aug 21, 2012 at 03:18 PM

ok... ive just read through it and, How do I implement this into my script? im ultra mega bad when it comes to scripting!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Aug 21, 2012 at 03:18 PM 0
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Flashlight turning off, but not back on again? 4 Answers

Flashlight Flickering 1 Answer

Flashlight effect 2 Answers

2D Lighting Effects in Unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges