How To Make A Realistic Flashlight FPS In Unity In Under 30 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hello everyone by the end of this tutorial you're gonna know how to make this realistic looking flashlight and I've tried to explain everything thoroughly but if you have a question you can always ask me in the comment section so without further Ado this is code at all and let's jump in the tutorial okay so the first thing you want to do is to open up Unity Hub I'm using version 3.5.1 and what you want to do is to create a new project and you want to scroll down until you see 3D hdrp which is the high definition render pipeline and you want to select this because the certain effects we're going to use will require you to have this template so I'm going to click on it and you need to download the template for the first line and after that you want to give your project a name I'm just going to call this realistic flashlights and click create project and this could take some time so you might want to grab some coffee for yourself and so yeah I'll see you when it's done so once you're inside Unity you can go ahead and close this hdrp wizard window and the first thing I'm going to do is I'm going to go ahead to the hierarchy Tab and I'm going to right click and create a new empty game object and I'm going to call this player now after that you want to go ahead and create another empty game object you want to call this head go ahead and drag the camera under the head now click on the project Tab and what you want to do is you want to create a new folder call it scripts we're going to be putting our scripts into this folder and right click again create a new c-sharp script this is going to be our script that we're going to be using to move our player around so I'm going to call this movements controller I'm going to open this now once your code has loaded up what you want to do is to add a reference to your rigid body private rigid body I'm just going to call this RB you want to get that rigid body in the start method by calling the get component method and we also need a reference to the head of the player so I'm just going to call this public transform because we only need the transform of the player's head and I'm just going to call this head now below the rigid body we also want to create a new private Vector 3 call this direction and in the update method we want to assign the direction to the input of the players get access Raw horizontal and we want to multiply this by the heads right transform I'm going to add that to the input.getaxis Raw vertical input of the player multiplied by heads forward transform now if you want to learn more about this code I've made a separate tutorial about FPS controllers so you might want to watch that first the link will be in the description so we also need a player speed so I'm just going to call this public float player speed now because I want to move the player around smoothly I'm also going to create another variable called float player acceleration I'm going to assign this to two now down below what we want to do is type rb.velocity equals to vector3.lurp which stands for linear interpolation and as the first parameter we want to pass out rb.velocity and we want to linearly interpolate between rb.velocity and direction multiplied by player speed and the speed of that is going to be defined by player acceleration Times by time dot Delta time so it's consistent across all devices if you jump back to Unity you want to drag the code to the player and also you want to drag the head to the script so we have a reference to the head of the player and you also want to add a rigid body and a Collider I'm going to add a capsule glider I'm going to press the I icon on the sky and fog so it's not visible and what you want to do is to reset the transform of the camera and on the head you want to bring it higher a little bit I'm going to type 1.8 on the y-axis now [Music] scale this capsule collider a little bit so it's representing the size of our player now what you want to do is to add a plane or a ground so our player can stand on we want to reset the transform I'm going to adjust the collider a little bit so it's nice and accurate now I'm going to go to the game Tab and press play now if I pressed forward we're going to start rolling around so what you want to do is in the rigid body component you want to go into the constraints and tick all of the axes for the frizz rotation so now if you press play we're moving very very slow so I'm going to increase the acceleration by 4 and player speed to 8. so now you can see we're moving much more reasonably and if I go on the edge you can see I can fall off now a way to make this code better is to normalize this direction and that that is going to prevent the player moving faster diagonally and the next thing you want to do is um I'm going to press enter here to bring this line down so you can see this better you want to add this uh Direction with the rb.velocity.y axis and the reason you want to do this is because when you're writing this code you're essentially saying equals to this value so you're overriding the y-axis you're basically overriding the gravity of the rigid body so to prevent that you want to also add it to its own y-axis and right now it's giving us an error and but that's because we're adding a vector 3 to a float and to fix this you want to multiply this to a vector 3. up so it's also a vector three and the next thing you want to add is the jumping functionality and to do that I'm just going to come here and say if input Dot get button down and the button name is jump and also we need to create a method called is touching ground if these two conditions are true we want to jump and we want to create a new variable called public float and jump Force just going to assign it to something like six now and you want to add to this velocity and not assign it so I'm going to say plus equals to jump Force times Vector 3 dot up because remember jump force is just a float and we need to convert it into a vector 3. now let's create this method I'm going to create it down here pry weight Boolean it's going to return a Boolean so type bull is touching ground and this is actually a really simple way of checking if we're touching the ground or not again I've talked about this in my FPS tutorial so if you really don't know how to do things work you might want to watch that tutorial first so we want to return physics dot check box and we want to check a box at the player's feet so that's going to be transformed that position and the size of this it really depends on your level and your character size but I'm gonna go for 0.05 on every axis now for the next one I'm just going to type quaternion dot identity and for this layer mask we're going to need to create a ground layer so I'm just going to call this ground layer and we want to go up here create the variable public layer mask ground layer so now if we go back to Unity if you clicked on the player you can see there's a ground layer slot here and at the moment we do not have a grant layer created so what you want to do is to go to the layer up here and you want to add a layer it doesn't matter what you call it but I'm just going to call this ground and back on the player I'm going to assign it under player script now you also want to assign it to the ground that you're playing on so just click on the ground and assign it to the ground layer now if you press play if I press space you can see nothing happens and that's because we need to increase the size to something like 0.1 and now if I pressed space you can see I can jump and I can jump only once while I'm in the air so that's the player movement controller and the next step is to create the camera controller and for that I'm going to actually create a new script and call it camera controller we're also going to be putting our flashlight controller into this script so inside the script what you want to do is to create a reference to the camera I'm going to call this underscore camera and also another variable which is going to be the sensitivity of our camera so I'm just going to call this camera sensitivity assign it something like 200 you should also play with that value and also another public float and I'm going to call this camera acceleration and assign it to something like five now you want to come down and create two variables these are going to be pry weights I'll type float and for each axis of the camera that we're going to be rotating which is going to be the x-axis and the y-axis we want to create a new variable and call them rotation x-axis and copy this line we're just going to call this one rotation y-axis now the camera is going to be rotated on the x-axis and the head is going to be rotated on the y-axis so in the urban method what you want to do is type rotation x-axis plus equals input Dot get access and we're going to call this mouse X now there's also a new input system which is technically what you should be using but for simplicity's sake I'm going to be using the alt input system so just be aware that this is not the best way of getting the player's input but this is something that's going to work and easy to understand and later on you can change it with the new input system if you want so we want to multiply this by camera sensitivity and also multiply that by time dot Delta time I'm going to do the same thing for the rotation y-axis but this Zone oh okay so you actually need the Y input of the mouse or the x-axis and the X input of the mouse for the y-axis rotation so it's kind of reverse now what you want to do is to type transform that local rotation this is going to expect a quaternion so you need to convert your values into quaternion and there's actually a handy method inside the quaternion class called Euler and remember for the head we want to rotate it on the y-axis so for the x-axis I'm just going to type zero for the Y rotation y-axis and for the Z we're just going to type zero now we want to do the same thing with the camera so camera dot local rotation quaternion dot Euler rotation X and 0 for the rest now if you go back to Unity and drag the camera controller script to the player and drag the camera into the camera slot and if you played this you can see I can look around the camera is inverted but that's okay uh but the thing is if you keep looking up eventually you can do this 360 Degrees thing and you can just keep going and going and the reason this is happening is we need to limit the camera rotation on the x-axis so to do that it's actually really simple you want to take the rotation x-axis and there's actually a handy method inside the math F class and it's called clamp so you want to clamp this value between minus 90 degrees and positive 90 degrees and also you need to invert the camera so I'm just going to put a minus here and now as you can see everything is working correctly I can move towards the direction I'm looking at I can jump and as you can see the camera is kind of like locked to our cursor so for example I can look around really fast and to make this transition a little bit more smooth what you want to do is to linear interpolate between these values and I'm actually going to move this line up here and on the local rotations what you want to do instead is that quaternion.lurp and you want to learn between local rotation and you want to paste quaternion.oiler and the speed of that is defined by camera acceleration multiplied by time dot Delta time and I'm going to add a new line here so it's readable so we're essentially linearly interpolating between the current rotation of our head to the new rotation which is calculated here by camera acceleration speed so we want to do the same thing with the camera here I'm going to cut this here I'm going to assign this quaternion.lurp once again camera dot local rotation I'm going to add a new line add our new rotation and the speed of that is going to be camera acceleration multiplied by time dot Delta time so now if you go back to Unity press play you can see I can look around and it's kind of like a a bit of delay here and it's it's really smooth and it's kind of realistic it's not just snapping to my cursor and you can play with this acceleration value for example if I put it to like two you can see now it's really slow and it might be the effect you're looking for it really depends what you're doing but I personally think it's really cool so now it comes to coding our flashlight logic and the first thing I'm going to do is I'm going to go to the arsene view and I'm just going to scale this ground a little bit more so we have more room to play on and I'm gonna add a new plane reset his transform I'm going to rotate it 90 degrees on the x-axis and scale it a little bit on the x-axis so we have a wall to shine our flashlight on now the first thing you want to do is to go to your player and create an empty game object and call it hand and under that hands you want to create a Spotlight which is going to act as your flashlight and I'm going to bring this hand a little bit higher that should be good now I'm going to delete my son in the scene so we can see the flashlight and I'm going to increase its range a little bit so it's visible a little bit better now if you played this the flashlight is not following the camera at all and so to do that we need to go back to our code and back to up here I'm going to create a new transform variable this time it's going to be called hand and what you want to do is to just like these two before type hand dot local rotation and assign this to quaternion dot Euler rotation.x rotation dot Y and zero for Z so now if you go back to Unity and go to head and you want to drag this hand into the hand slot and now if you plate this you can see the flashlight is moving but the rotation on the x-axis is inverted so to fix that what you want to do is do put a minus in the x-axis now I'm also going to hide the cursor once we play the game and for that you want to go to the start method and type cursor.lock State equals the cursor lock mode dot locked now back in unity you can see I've also added these cubes so we can navigate the scene better and by the way if the scene is too dark for you and you want to see what you're doing you can click this light bulb button here and it's going to toggle between the light scene and if it's off it's just going to use the camera light so if we go and play this I can look around and you can see that it's got this responsiveness to it which makes it feel realistic and to intensify that effect what you can do is to actually lower the camera acceleration to like something like two and now if you play this again you can see my flashlight is always moving first and then the camera if you think about it that's how this would work in real life and so that's what makes it realistic and there's one thing you can do to make it more realistic and that's to add a cookie essentially it's just a texture that you're going to apply to your flashlight and that's going to give you the illusion that it's being projected behind a I don't know glass and for that I have this cookie texture here and it doesn't matter what you use just make sure it's a PNG and the link for this will be in the descriptions below so I'm going to drag this into my Unity project and to apply it simply just go to your flashlight and down here you can see there's a spot called cookie you want to drag this texture under and there you go you have a simple flashlight effect and one thing I'm going to do is to actually increase its radius a little bit so it looks a little bit more realistic that should be fine okay so now we're almost done and there's one more thing you need to add and that's you kind of want to add a little bit of volumetric lighting to this scene and that's honestly the only reason we're using the high definition render pipeline here and it's actually really simple all you need to do is to go to your Spotlight or flashlight and down in the volumetrics you want to increase this multiplier a little bit and you want to bring this flashlight a little bit further from the camera you really need to play it with these values to see what works best for you so as you can see I'm moving a little bit closer to the player now you want to make sure that you're adjusting the x and z position of the flashlight on the flashlight itself and only adjust the y-axis on the hand this will make sure that the flashlight will always remain in its position even if you rotate it like 180 degrees so on the flashlight itself I'm going to move it a little bit to the side here so it looks like that you're holding it on your hand I'm going to move it a little bit closer to the player and that should be alright one thing you can do because you can see it kind of looks blurry now you can go to your sky and fog volume and go all the way down to where it says fog you want to enable quality down here and set it to high and now you can see it kind of increases its quality and the other thing is if this volumetric lighting is not enough and you want more you can go to the fog attenuation distance and you can enable this and if you decrease it you can see it's kind of increasing the effect of it and I think this should be fine for now and also I would like to give it a little bit of cold temperature here to give it a bit more flashlight effect so yeah that's it everyone I really hope you enjoyed this tutorial I really hope you learned something and please consider subscribing and liking this video sharing it with your friends and until the next tutorial I will see y'all soon foreign [Music]
Info
Channel: Code It All
Views: 4,562
Rating: undefined out of 5
Keywords: horror, unity, game, scary, horror game, flashlight, flashlight in unity, realistic, how to make a game, how to create a game, how to add flashlight to game, tutorial, unity realistic flashlight, responsive flashlight, unity horror game, unity horror tutorial, simple, fast, unreal engine, ue5
Id: WXWIK5J4hnw
Channel Id: undefined
Length: 26min 22sec (1582 seconds)
Published: Sat Sep 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.