Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Flash light

Discussion in 'Scripting' started by Dark_Lorde15, Dec 27, 2015.

  1. Dark_Lorde15

    Dark_Lorde15

    Joined:
    Dec 27, 2015
    Posts:
    7
    How to make a flashlight that turns off when you press the mouse right click and have battery that runs off after 3 minutes of using?
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    use an IEnumerator to create a timer for the battery and a function that will do the turn on and turn off of the light. The rest should be easy using some google searches.
     
  3. Dark_Lorde15

    Dark_Lorde15

    Joined:
    Dec 27, 2015
    Posts:
    7
    i didn't understand any thing im a noob.
     
  4. Rombie

    Rombie

    Joined:
    Feb 18, 2015
    Posts:
    282
    I would suggest you start with the beginner tutorials first. Working through those will teach you the basics of unity and bring you up to speed; allowing you to create your own game.

    Or you could purchase assets within the asset store and import them within your project.
     
  5. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    (Before I continue I need to mention that I am no pro but I simply try to help.)
    What I would do is:
    1) Create a light within your game and place it next to your character.
    2) Create a script which looks something like this:
    Code (CSharp):
    1. //All I made this script for is to turn a torch On/off!
    2. //Please do the rest yourself. I think Delta.DeltaTime would be useful! Start there!
    3.  
    4. //In the inspector drag & drop the light into the script.
    5. public GameObject torchLight;
    6. //This will store a reference to the torch intensity (How bright it is, 0 = "off")
    7. private float torchIntensity;
    8.  
    9. //This is how bright the torch will be in its "on" state
    10. //Is 1 maximum? I don't remember
    11. private float torchOnIntensity = 1f;
    12.  
    13. void Start()
    14. {
    15.     //Let the variable know what to store.
    16.     torchIntensity = torchLight.GetComponent<Light>().Intensity;
    17. }
    18.  
    19. void Update()
    20. {
    21.     //If left mouse button is pressed down than change the intensity to 0
    22.     if (Input.GetMouseButtonDown(0))
    23.     {
    24.         //If the torchIntensity does NOT equal maximum, than it does now!
    25.         if(torchIntensity != 0f)
    26.         {
    27.             torchIntensity = 0f;
    28.             Debug.Log("Torch Intensity Changed to 0.");
    29.         }
    30.         else
    31.         {
    32.             //If torchIntensity equals 0, now it equals maximum
    33.             torchIntensity = torchOnIntensity;
    34.             Debug.Log("Torch Intensity Changed to Maximum!");
    35.         }
    36.     }
    37. }
    3) Now go watch some C# tutorials!
    There may be a couple of small errors, please let me know as I a noob still and all help is appreciated.
    Thanks.
     
  6. Dark_Lorde15

    Dark_Lorde15

    Joined:
    Dec 27, 2015
    Posts:
    7
    Thank you for this but it didn't work.
     
  7. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    How did it not?
    What errors did you get?
    Did you drag your Light object into the Script?
     
  8. Dark_Lorde15

    Dark_Lorde15

    Joined:
    Dec 27, 2015
    Posts:
    7
    it worked now THX:)
     
    TaleOf4Gamers likes this.