[Unity Tutorial] Beginner Flashlight Tutorial! (Using 'F' to Toggle On/Off)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up youtube today i got another tutorial for you and this one does involve a little bit of coding but it's not too bad um this one is a actually it's another request i got and the person who requested it wanted to have a toggle flashlight that the player could use so basically you press f turns the flashlight on you press f again and it turns it off cool so how do we do this well the first thing it's pretty simple um right on our first person controller here um this one i got from the unity asset store as part of the standard assets pack you guys can get that for free it comes with a bunch of stuff um probably the best thing it comes with is actual setup first person controller you can use so on our first person controller let's expand it and right click on the character because that's where our camera is and we're going to add a spotlight to our character and this is really nice because it adds it right where the camera is looking so basically it's like right in front of you at all times and it uh looks really nice cool um i'm actually gonna rename this call it flashlight and uh we do get an error from an indirect multiplier so let's just take that off make it zero get rid of that error i'm not sure why it does that but uh it just does um on real time lights cool um so what we can do from here is we can actually adjust the intensity uh spot angle all that stuff really depends on what you guys want um i found that at least for my game an intensity of around seven and a spot angle of around 50 works pretty good um the range i'll just keep it at 10. you guys can change that as you can see it kind of you know adjust the range there um the color i kind of like going with a more kind of grungy-ish yellow color kind of gives it like an older style flashlight feel and one other cool optional thing you guys can do to give your flashlight a little bit more kind of realism is you can put a cookie on there so on our flashlight this spot for cookie let's take this and drag it right in there and as as you can see instantly just makes it look more realistic um this cookie i'll leave a link from the for for you guys in the description if you want to download this you can um if you guys download it because i'm not sure um if for whatever reason the texture type isn't cookie just make sure it is so just click on it and make sure the texture type says cookie or else it's not going to work awesome so we have our flashlight in there and let's just go ahead and play this see how it looks yeah that looks pretty good um like i said you guys can adjust that if you want you want to dim it make it more bright it's really up to you awesome but how do we toggle this so to toggle it on and off we are going to need a little bit of code so let's create a new c-sharp script and i'm just going to call this cool so let's go ahead and open this in visual studio all right cool so the first thing we want to do is under the public class let's add a serialized field and what this is going to do is actually create a drag and drop for our light which you'll see in just a bit it's going to be up type game object and let's call this flashlight light um now i can't call this flashlight because if i call this flashlight it's going to interfere with interfere with the actual name of the script and it's going to confuse the hell out of unity it's not going to work so let's call it flashlight light you guys can call it whatever you want doesn't really matter just don't call it you know don't call any game object the same name as your script otherwise it's uh not going to work i learned that the hard way uh let's make a public sorry a private bull and this is going to be flashlight active and we're going to make this false so basically a bull is basically a true or false and unity is going to check to see if whether this flashlight is on if it's true and if it's off it's going to equal false so we're going to use that in just a second as well awesome so under this void start function we're going to say flashlight light which is our actual game object the light we just made dot game object dot set active and we're going to make this false to begin with now if you guys want your flashlight on when the game starts just make this true um i personally think it's better when it's off that way um the player is forced to turn it on when they need it and under the update this is actually where we're going to make it so if you press f it turns on and if you press f again it turns off cool so let's let's uh do a couple if statements here so i'm going to say if and leave a little space you don't have to i just think it looks cleaner input dot get key [Music] down and key code is going to be f so this is basically saying if the user presses the f key on the keyboard and we need one more if statement if they press that and if the flashlight boolean is false so we're going to say if they press their keyboard you gotta spell that right and if the flashlight is already turned off then we can say um let's uh leave a little space there there we go we're gonna say uh flashlight light dot object dot set active uh true okay and last thing we need to do is change that boolean from false to true flashlight active equals true so basically what we did there is we said if the user presses f and the flashlight is already disactivated or false meaning it's not turned on let's set it to true so the flashlight turns on and the last thing you do is create an else statement and say else if the user basically presses f again um actually to make it easier we can just copy and paste this right here and set it to false so basically when f is pressed for the first time flashlight turns on else if they press f again this is just another fancy term for saying like um you know the opposite of what you want to say here basically then you turn the flashlight off and that is pretty much it guys um let's go ahead and save this and let's go back to unity and take our flashlight script that we just made drag it onto the first person character where our light is click on that and if we oh i got an error there what am i missing serialize oh um spelling is very important in coding guys which i've also learned the hard way if you guys spell literally anything wrong which unity is nice enough to tell you but uh yeah stuff won't work so make sure you spell right and as you've noted by now i am a horrible speller but anyway now our uh now our serialized field is here this is what serializing and feel does it actually allows you to create a bar that you can drop stuff into so we want to drag our flashlight that we made our light into there and now let's play this and see what happens awesome so as you can see our flashlight is off we press f and look at that guys it turns on press f again it turns off press that again and turns on off on off and on awesome so if you guys like this tutorial uh please hit the bell icon to stay up to date stay up to date drop a like and subscribe for more and i will see you guys in the next one peace out
Info
Channel: StupidaZZle
Views: 26,358
Rating: undefined out of 5
Keywords: unity, flashlight, unity3d, tutorial, horror, horror game, how to, toggle, stupidazzle
Id: vRKrg9Ku8Aw
Channel Id: undefined
Length: 8min 31sec (511 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.