Search Unity

Flashlight?

Discussion in 'Scripting' started by edplane, Oct 11, 2009.

  1. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Hi, I need a script to apply to a spotlight so that It will turn on when the 'f' key is pressed, and off again, to represent a spotlight.
    Anyone?
     
  2. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    Make an empty gameobject and apply a script that checks for when the user presses the 'f' key. Then when the key is pressed, deactivate the children to that empty gameobject. Make the spotlight a child of the empty gameobject.
     
  3. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Lol, I literally just tried to make and completed a flashlight script before you posted 8).

    Do what the guy above me said, and if you don't like that method of the flashlight script try making the intensity set to whatever. So when you press f, the intensity either increases or decreases depending on what it did last time.

    Hope I helped.
    :D
     
  4. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    instead of an empty gameObject, why not the flashlight(cylinder) itself?
     
  5. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    The above example was referring to if you did not want to use the cylinder, but both will work.
     
  6. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    If you have a script that is attached to the flashlight that deactivates itself when you press the 'f' key, then when you press the 'f' key again, it won't go back on. Because the script is deactivated with the flashlight and will no longer check for user input.
     
  7. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    ok, heres what I did. I got rid of the game object, it interfered with the script. I set the light to the camara in the right position, then, made it rotate around the y axis 180 degrees, so its not visable to the camara:
    Code (csharp):
    1. var rotate = Vector3(0,180,0);
    2.  
    3. function Update()
    4. {
    5.     if(Input.GetButtonDown("Flashlight")) {
    6.     transform.eulerAngles += rotate;
    7.     }
    8. }
    This works fine, Im only good at writing transforms. :D
     
  8. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    This won't work for a multiplayer game though. Why not just set the intensity with the code?
     
  9. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Because I dont know the code to use. It works for a FPS single player game, but not for a third person or multiplayer game. what code do I use to adjust intensity?
     
  10. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Ah, ok. Let me show you :D .

    Code (csharp):
    1.  
    2. var flashlightOn : boolean = false;
    3.  
    4. function Update () {
    5.     //Checks if the boolean is true or false.
    6.     if(flashlightOn == true){
    7.         light.intensity = 1;//If the boolean is true, then it sets the intensity to what ever you want.
    8.     } else {
    9.         if(flashlightOn == false){
    10.             light.intensity = 0;//If the boolean is false, then it sets the intensity to zero.
    11.         }
    12.     }
    13.    
    14.     //Checks if the F key is down and whether the boolean is on or off.
    15.     if(Input.GetKeyDown(KeyCode.F)  flashlightOn == false){
    16.         flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.
    17.     } else {
    18.         if(Input.GetKeyDown(KeyCode.F)  flashlightOn == true) {
    19.         flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.
    20.         }
    21.     }
    22.    
    23. }
    24. }
    25.  
    26.  
    27.  
    Sorry for the comments being carried to the next line, they are long. :D
     
  11. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    What exactly does a boolean do?
     
  12. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    never mind, I know now that a boolean is true or false.... but sometimes the syntax confuses me, like , ==, and all the {}{}{} its hard to understand what gos where and why.:p
     
  13. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    A boolean is a statement of true or false. That is all. Basically, what this script does is check if the boolean is true or false, and does the best action for the value of the boolean.

    It seems you are not just a noob to scripting but to programming in general, am I correct?

    If so, you may want to learn the very basics of a programming language before continuing. It would only take a couple of hours!
     
  14. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Yea, super noob. I understand basic transforms and JavaScript syntax, but still have a hell lot to learn! What do you think the most complex script in a FPS is?
     
  15. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Lol, well from what I have seen of the mouselook script of C# it might just be that :D.

    But other than that considering that you don't have to script that, it completely depends on what you are doing in the game. What type of weapons and is there going to be AI? I would guess AI, but if you are trying to make an FPS you might want to check the tutorial on it! However, only use this as an example because I have heard it is done pretty badly.
     
  16. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Yes, weapons and AI. The AI in the tutorial isent bad, its just simple. As far as Ive seen, a lot of Unity-based Fist Person Shooters arent very good, I guess no one wants to spend too much time on them. However for me, the hardest part would be the GUI and LoadLevel scripts, so that everything the player changed in the previous level, can still be accessed(eg, dead enemys, blood splat, broken crates, general position of objects, ect..) As for AI, there will be a lot of different ones, for each creature, but I want to make a zombie one. Plus, the character and gun animations themselves, what really makes a game realistic, is lacking in other Unity FPS's, and would probebly be difficult. Wow, I have high ambitions, and It will be quite a while until I can acually get that far. It just makes me feel stupid now.. lol :wink:
     
  17. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    272
    @ edplane. here is my light switch script i made for my game. its a very simple script.you can turn your light on and off with it.
    Code (csharp):
    1. function Update() {
    2. if (Input.GetKeyDown("f")) {
    3.     if (light.enabled == true)
    4.         light.enabled = false;
    5.         else
    6.         light.enabled = true;
    7.     }
    8. }
     
  18. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    OK, It works, but this one enables and disables the light function. I dident even know there was a code to do that! thx!
     
  19. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Why did I not look further down the page of the scripting reference for enabled? Oh well, thanks that helps me too.
     
  20. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Whats with your signature? I would rather learn everything in one second than 4 years! XD well, I guess things just work like that! Any way, whats the point of == instead of just =?
     
  21. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    The point of == is to specify that you are comparing two things where as = is setting one thing.

    And I like learning slowly because learning is fun, so it is better to learn slowly so I get the most enjoyment.
     
  22. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Thats you. My enjoyment is the satasfaction that I made that awesome game there. In fact, this zombie game I make, the funds I raise from selling it(if I ever do) will go to the Pro version, making my ultimate goal real. Which is to re-create Half-Life (with permission from VALVe), give it away for free, or sell at a low cost, but I want it to be as AWESOME as the new mod, BMS, witch is also a re-creation. And keep environmental accracy, Including all the functions of the menu, but i dont know anything about the GUI system, soooo its gonna be a few years untill I even complete my zombie game, longer for the HL remake, but, by then, I could be the one keeping it alive. Unless 2012 is for real, lol. :D
     
  23. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Also, your script being the basis, i am trying to make a sound whenever the lights are turned on or off:
    Code (csharp):
    1. var soundon : AudioClip;
    2. var soundoff : AudioClip;
    3. var power : float = .1;
    4. var flashlightOn : boolean = false;
    5.  
    6. function Update () {
    7.    if(flashlightOn == true){
    8.       light.intensity = power;
    9.    } else {
    10.       if(flashlightOn == false){
    11.          light.intensity = 0;
    12.       }
    13.    }
    14.  
    15.    if(Input.GetButtonDown("Flashlight")  flashlightOn == false){
    16.       flashlightOn = true;
    17.    } else {
    18.       if(Input.GetButtonDown("Flashlight")  flashlightOn == true) {
    19.       flashlightOn = false;
    20.       }
    21.       if(Input.GetButtonDown("Flashlight")  flashlightOn == true){
    22.         AudioSource.Play(soundon);
    23.       } else {
    24.         if(Input.GetButtonDown("Flashlight")  flashlightOn == false){
    25.             AudioSource.Play(soundoff);
    26.         }
    27.       }
    28.    }
    29. }
    But this isent working. Whats wrong?
     
  24. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Finally, I have it figured out.

    Code (csharp):
    1.  
    2.  
    3. var soundon : AudioClip;
    4.  
    5. //Checks if the boolean is true or false.
    6.     if(flashlightOn == true){
    7.         light.intensity = 1;//If the boolean is true, then it sets the intensity to what ever you want.
    8.     } else {
    9.         if(flashlightOn == false){
    10.             light.intensity = 0;//If the boolean is false, then it sets the intensity to zero.
    11.         }
    12.     }
    13.    
    14.     //Checks if the F key is down and whether the boolean is on or off.
    15.     if(Input.GetKeyDown(KeyCode.F)  flashlightOn == false){
    16.         audio.PlayOneShot(soundon);
    17.         light.color = colorUsed;
    18.         flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.
    19.     } else {
    20.         if(Input.GetKeyDown(KeyCode.F)  flashlightOn == true) {
    21.         audio.PlayOneShot(soundon);
    22.         flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.
    23.         }
    24.     }
    25.  
    26.  
    This works, make sure you add the sound to the spotlight and make sure to set your sound variables to the sound. If you need an example, just hollar.
     
  25. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Yes it works! But what does PlayOneShot do anyway? While on the subject of sounds, I made this script to play a sound track. the object is on top of the real floor, and is a trigger, and moves away from the real floor when playing so is not to be triggered again:
    Code (csharp):
    1. var object = Vector3(0,-10,0);
    2. var soundtrack : AudioClip;
    3.  
    4. function OnTriggerEnter()
    5. {
    6.     audio.PlayOneShot(soundtrack);
    7.     transform.position += (object);
    8. }
    The object, also, has to have an audio source with no dopler or other effects so the volume is constant, no matter where the player is. wow, I made that script up and de-bugged it on my own! cool, lol
     
  26. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Lol, good job. PlayOneShot basically means that it is played once. Whereas Audio.loop would play it over and over and over again!
     
  27. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Oh, OK. But that script is good for soundtracks, Now i need to work on other sounds, whith rolloff factors and what not.
     
  28. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    NOW im having problems. the rolloff isent working here:
    Code (csharp):
    1. var volumeLow : float;
    2. var volumeHigh : float;
    3. var rolloff : float;
    4. var sound : AudioClip;
    5.  
    6. function OnTriggerEnter()
    7. {
    8.     audio.PlayOneShot(sound);
    9.     audio.rolloffFactor = (rolloff);
    10.     audio.minVolume = (volumeLow);
    11.     audio.maxVolume = (volumeHigh);
    12. }
    Ideas?
     
  29. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    I just tested the rolloff that is not used by script and it seems to not do anything even with a value of 4000.
     
  30. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Hmm, thats weird, maybe try differnt syntax?
     
  31. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Actually, i just tested again. Make sure that it is not attached to your camera or it wont work and the sound file has to be set to mono for it to work.
     
  32. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    You are RIGHT! Thanks! So I guess In-Game sounds should be mono, and soundtracks stereo. Thanks!
     
  33. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Hey no problem, your questions build my skills as well.
     
  34. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Ha, sooo how great of a scripter are you? Have you made or are making any games yet?
     
  35. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Have I done whatever and stuff. Well, when working with flash it was a lot easier than this language. However, from looking at the examples all over this forum and the website I can learn quickly. And I am making a game that has no objective while learning the code. Basically you are a sphere with a flashlight and are on fire. You can walk run jump and move. And there is a collision. How good are you at scripting, I mean are you learning quickly?
     
  36. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Horrable. I understand basic syntax and transforms and such, but still learning, slowly, and sooo yea. as for games, i have a pointless game set up to test scripts, a zombie game, to complete whenever I make all the scripts, and a HL re-make, several years down the road to complete. :wink: Ha, acually, HL was revolutionary for its time in that there where no cut-scenes, using scripted sequences instead, and all the changes the character made in a previous level where still there if you went back, and so on; so all that will take a looooong time! :roll:
     
  37. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Look on the bright side. Half life would just be too hard without unity. SO since you have unity, you don't have to worry about not ever being able to complete it :D .
     
  38. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Damn straight it would. My first thought was to use Id Tech3, but I couldent find an SDK for it, but now that I have Unity, Im glad cause IdTech3 would probebly not be as easy as Unity, everything separate, but everything I need to do in Unity is right there, in one package, witch is probebly why its called Unity in the first place, but, still, I think Unity could be a bit more adaptable. :roll:
     
  39. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Yup, unity is awesome. Seems this topic has gone off topic.
     
  40. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Ha! Yea. First its about lights, then sounds, then learning scripting, then ambitions, and now Unity! Kewl, but I think my question is answered. But... what about light cookies to make the light look real?
     
  41. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    What would you like to know about them?
     
  42. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    How to make them... is it like a simple 2D texture?
     
  43. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Try testing things out. If you haven't figured out by the time I get home from school, I will see if I can help. :D
     
  44. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    I couldn't figure the cookie out :( .
     
  45. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    LOL so what school are you in?
     
  46. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Edward Milne Community School (highschool). A good school with no programming courses :( .
     
  47. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Ha, what, freshmen, sophmore, junior, senior? I, myself, am a freshmen... lol the nerdiest around..
     
  48. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Lol, cool. I am in grade 11. I am going to graduate next year.
     
  49. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    lol, Junior, I'll be 15 next year and get my learners permit, so I can drive, and a work permit, so I can actually make some money. lol. Plus, I hope to get a MacBook pro on my b-day, and replace this old iBook... Would you get the 2.23 GHz one or the 2.53 GHz one? I really just want to play L4D, and I see that the lower-end one plays it quite well. Maybe upgrade that to 8 gigs of RAM? Either way, anythings better than a 6-year-old PPC based 1.2 GHz computer! Ha, what do you have?
     
  50. KHopcraft

    KHopcraft

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    If I were going the way of mac, I would get the 2.53 ghz. But I would rather get a windows gaming laptop and hack it later to run OS X. :D