Unprofessional Guide About How To Make FPS Game In Unity - Fake GDC Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this video we're going to talk about how to make fps games in unity the reason why we have this video is because there are quite a lot fps tutorials on youtube but they're mostly for making prototype or demos and if you're going deeper into making an actual fps game you probably would find facing a lot real issues i've been making fps frameworks for some time so i want to share some of the ideas and tricks and i don't have time to make a pretty long tutorial for each line of code so i made this powerpoint as a gdc style presentation so as a gdc style presentation i'll introduce myself first i'm ryan by i've been working in perfect world in beijing for some years but we don't make fps games in the company and i also work on my solo projects currently i'm making a fps zombie game and here is the goals we want first off there are so many kinds of fps games and so many ways to make fps games so it's not about right or wrong how you do things differently but there's always some essential things we need here first we want a reliable fps system and we need the game to be smooth and responsive then we need a full body of the character model also we're not doing like triple a level animation driven workflow and we're not going to talk about details of gameplay as there are already a lot tutorials about that on youtube this is the overview of the video before we get started i have to say that i have never worked in a large fps project nobody told me what to do so all these tricks are based on my own understanding and experience in my solo projects and each fps game might differ from each other so it probably can't apply to every game but it can still give you some ideas and another thing is the terms player and character in this video player means controller and logics related stuff like input movement player status like health and weapons character stands for visual presentations like the models animations and ik it's just my own custom so you won't get confused in later parts of the video let's see the over solutions of fps games actually there's nobody tells me this but you could often see a lot fps games or fps frameworks generally going differently in these two paths the art driven and code driven the art driven is that you have a full body character which contained a camera bone near the head and your first person camera will be under or driven by this camera bone what you see is what you can see from the head of the character it's a quite a realistic way to make fps camera and very often seen in triple a single player fps the artist will make models and animations and these will decide how the camera swings in game it requires very high quality animations and might be less responsive for gamers to control the code driven is that the first person camera will be controlled procedurally the first person hands full body character and first person camera will be all under character wrote they're merged together by code to give correct visual result and you don't need a full body character if you don't want to the camera instead of calculate the rotation of camera manually i use the easy way to control camera make the hierarchy of camera as a turret-like structure you have a yaw and pitch transform as parents of the camera so each axis will be rotated separately it's very simple and reliable and if you want to give extra modification to each rotation it would be very easy to do the camera smoothing we have a lot argues about if mouse smoothing is good or bad over the years but gamers don't know what smoothing actually works we as developers don't know either as each game might have different way to do it and they all call it smoothing in the setting menu some really sucks some works actually pretty good some games already have smoothing during hardware input some games can have smoothing turned off but they still have an internal smoothing to make the control feel good which gamers don't realize this at all so let's say on windows first we turn off the mouse acceleration entirely if you don't know what mouse acceleration is you better know it before making fps games now we use the mouse x and y values and apply to the camera directly any tiny movement on the mouse pad will give your camera a little bit itchy moving result then i keep track mouse movement of the last few frames and calculate an average mouse movement apply it to the camera rotation as a way of smoothing it actually works pretty good the itchy feeling is gone and you can still do flicker shots in game here's the averaged frames of three i use an array and index to keep track of the mouse input each update the index would go 0 1 2 0 1 2 like that another way is to use interpolation use a variable as the final result of mouse movement and interpolate it to the mouse input of current frame in each update then use this variable to apply to camera rotation i've seen people doing like this but it will feel slippy in some cases so i would suggest just to use the average frame solution movement solution kinematic character controller is a very good asset from asset store to replace character controller of unity it has a very easy ground check to know if you're on the ground or in air so you don't need to do it by yourself it still works correctly when running up or down stairs as well and it supports all directions gravity like if you want to make a sci-fi fps that might have different surfaces to walk on and it would handle slip off the edge very good the downside is that kinematic character controller works in fixed update by the physics way so it's not very good to handle rotation for fps so the mixed way to do is to use code to rotate the camera by yourself and use kinematic character controller to move the player you should never change the kinematic character controller's rotation which by default is quaternion identity and by the logic the direction of the player should be decided by the direction of your camera here the simple workflow is that your own camera controller will apply mouse input into camera rotation you'll write your own movement controller to give movement input to kinematic character controller which will handles it automatically then in fixed update update the camera first person hands and full bodies position to follow the player's actually position animation solutions let's say we will just use unity's default animator controller to do the animations we need all animations to be transitioned smoothly so always use crossfade api to do it and we need animation state that can crossfade to itself for example when doing a melee attack repeatedly you want to play the same state from start by with smooth transition the good news is in unity 2020 you can crossfade to your own state by code and for animation events there's a nice asset called animator events that can add events to animation state and animator controller it supports different type of event like state enter state exit it supports trigger events at normalized time and it can be toggle if you want the event to trigger during exit for example you have an event at half of the reload animation but you do some other action to cancel reload and you don't want the event to be trigger if you cancel reload already so just untoggle this the event won't be called during animation exit then always use code to switch between the states here the reason why for example you're playing an unarmed running locomotion you want to switch to rifle running locomotion in your old locomotion your right foot is up front and you want when start play the new locomotion your right foot should also be at the front if you use the graph condition between the state machines on animator controller to switch states the destination state will always start at zero or fixed offset this will cause the feet go wrong during transition the glitch might be very obvious and can't be ignored but if you use code to crossfade to a different state you can set it what normalized time to start within the target state machine also when setting animator parameters to cause condition transition the transition might be delayed for a few frames as they're called in different internal updates this would cause glitch-like if you want to enable a certain layer and trigger a transition in it at the same time the layer can only be enabled by code and it's done instantly even by interpolating its weight but the transition might be late for a few frames and there might be one or two frames glitch showing a wrong animation and there's a bug that i've met that the trigger parameter sometimes don't work in multiple layers so the graph condition transition isn't a reliable way to switch states in unity make good use of blend tree like my character locomotion have two layers of blend trees the idol is at the same layer of walking and running blend tree the reason for this is that when strafing from left to right there's a brief moment the speed is zero but i'll keep blendtreon running when quick changing strafe direction and it won't goes back to idle when speed is zero if you take time to think about it you can have a more complicated blend tree for locomotion also a lot of state machines can be used to blend tree if you have sufficient animation clips when play turning animations use turn left and turn right as a blend tree use the angle as its parameter so for different angle it will give the correct turning correspondingly first person hands can have idle and aim down side idle as a blend tree as well anyway when you design animator structure you can have an extra try if it can be used as blend tree use multiple layers for complicated logics like showing rifle i'll play the show rifle both on base and upper layer at the same time because base layer show rifle is in place full body animation and upper layer is upper body mask of the same animation so when playing show rifle suddenly the players start moving the bass layer show rifle is gone and upper layer show rifle is still overriding the upper body layers have too many ways to take good use you can have quite a lot layers for different result i won't go too much about as it requires a dedicated design for the project and different type of additive animations to work with use parameters to control place speed of the state i think a lot of people already use it and it can be used for quicker reload animations animancer is also an animation solution from asset store i added to the powerpoint as i've been using it since the first version it's a replacement of mechanim the unity animator controller and it does have some shining points you can create dynamic animation states at runtime the states can crossfade to itself which was an extra point over mechanim before unity 2020 unity has jitter problem when switching quickly between two states repeatedly but animanser don't have this issue but there's still some cons about it you cannot crossfade a mixer state to itself event system and layer weight fading sometimes fail to work which means you have to have your own workarounds and it's not as stable as unity's animator controller it doesn't support empty state and it requires you to write a lot of codes so i've already switched back to unity's animator controller since unity 2020 well animancer is still an option if you're interested so let's see the general structure of first person hands the camera is under the character root transform a weapon root transform is under the camera each weapon is a transform under the weapon route then when switching the weapon there would be one weapon set active and the rest will be inactive and for each weapon it can be hands modeled with weapon model together with just one animator at route and they use the similar skeleton and avatar so you could change the mesh dynamically or use a general hands to spawn for each weapon and also a different weapon model for each weapon and weapon model should also have an animator and related animations of it to match the animations on hands the picture shows weapons from low poly shooter pack from asset store which also contains an older version pack if you have this asset you'll see how they design the art workflow i make my fps framework that can work with only three animations at minimum idle reload and fire other behaviors show hide weapon and bobbing can all be done by code procedurally and i also support using extra animations as an option to play show hide inspect blend tree idle and firing for the idol i put the weapon in the hierarchy and find the best local position and rotation for idle to be seen in the camera also the position rotation for ads which is aimed down sight the weapon still plays default idle intention but ads position would put the weapon close to camera to make it look correctly even if you use an animation as ads idle you probably would also need custom ads position for different optical sites because they need to show the front sight to be close to the middle of the screen fps player this should be the core component of your player controller you could have as many variables to keep track the player status if needed like if player is standing or crouching is moving or not moving what's the speed threshold to differ moving is playing sprinting ads reloading attacking in air and more and a lot of your player behaviors will take consider these status for example do you allow reload when sprinting if you don't then when you press reload button you should check if you're sprinting do you allow crouch when in air when you're sprinting would reload or attack will cancel the sprint do you want ads as toggle or hold do you want sprinting as toggle or hold when you are in switching weapon do you allow a new switch action to activate over it like quickly pressing q in counter strike and if you don't have a global ideas you how your behavior rules would be you could see the details of other fps games and make your designs ahead of time it's very important that you make clear decision of how your game would be it'd be very troublesome if you want to add new behavior when the progress of your project is at halfway like if you never consider about climbing ladders and it would be very risky to add ladder when you almost finished the game because i code to control the bobbing of first person weapon procedurally i use a global curve for bobbing i call it bobbing curve the player controller will maintain this curve in each update the range of the curve is from 0 to 360 degree in angle then goes on from zero again it will take consider the speed of movement like if you're moving fast the curve will increase fast if you're moving slow the curve will increase slow if you're not moving the curve goes up at all because the curve is between 0 to 360 the sine and cosine of it would be continuous and friendly for controlling bobbing like swing weapon left and right for each weapon it will contain several bobbing behaviors like idle bobbing yes idle bobbing if you have just a static bobbing pose instead of animation then you'd need to use code to swing the weapon when an idol so idle has a bobbing as well then strafe bobbing is for strafing left and right run bobbing is for running sprint bobbing is for sprinting ads bobbing is for ads idle jump bobbing for jump start and jump end all these bobbing have their own values like position and euler values for the maximum effect it will give to the weapon and each of them will calculate these results by the global bobbing curve and also consider what player status is usually the result is calculated by sign or cosign you could have extra modification as well and in each update when the calculations are done the weapon at all bobbings positions and rotations additively to the parent transform to make it act like swing the weapon now there's a very important thing we have to talk about avoid clipping into environments a lot tutorial will tell you to use two cameras one for the environment one for the first person weapon just don't do this way as it's very troublesome if you want correct shadow casting and double camera might give you a lot extra draw calls so as a replacement i'd recommend using this asset from asset store the first person view it uses a shader way to make the weapons rendered over environments and shadows still works correctly and all objects are rendered in just one camera see here's a goal we want like in every fps game one your first person weapon shouldn't cast shadow to environment as you don't want to see a shadow of hands in air on the ground that's easy we just turn cast shadows off on the meshes two environments can cast shadows to your first person weapon which means your fp weapon and environments as layers should all be in the light supported layer masks if you're using hdrp they all should be in the light supported light layers which means if you're using two cameras to render by different layers this will work your environment won't cast shadow on weapon and only the first person view can work this out three your first person weapon should always render over the environments yes first person view can do that in just one camera and there's another bonus feature it has that fpv plug-in supports an inactive camera to control first-person weapons field of view this is very helpful when you do ads and you could also avoid glitch to see outside of the first person weapon if player change the default camera's fov fast making fps hands this is a fast way to create first person hands and weapons within unity with the assets you can get from asset store so first assume you're not an animator or 3d artist at least not a professional one and you have some characters and a first person package but the hands doesn't match your character so you want to use your character's meshes and the first person packages animations to create a first person hands and animations that matches the character so how do you make animations without making them see here are the abilities we have in unity we can use editor scripts to create meshes from existing meshes and with unity api we can use editor script to create animation clips and we can use inverse kinematics which is also called ik in unity so the conclusion is that we can use ik to play animations and record them into animation clips so we can have animations for first person hands then here's the assets we need you could use alternative solutions like writing your own codes but this is a fast way to go forward the final ik it's the best ik plugin you have on asset store the fps mesh tool which can cut a humanoid character into parts of head hands and body puppet master we don't use puppet master here but it has some amazing scripts that can make any movement of transforms into animation clips umotion pro it's just what i use for post processing of the baked animation clips if anything don't show correctly i'll use your motion to modify the animation a little bit so you could use any alternatives if you like first we use fps mesh tool to cut the hands out you'll assign the head and upper arm bones to the editor window it will be done in just one click we can control the result with head and body set to invisible shader we just need the arms for first person hands it's a fps hands with weapon from low poly fps pack on asset store its animations will drive our hands to move so i'll put this fps hands under our character's route our characters wrote should have no animations to play if it has an animator component just set the animator controller to null so our hands can be freely controlled by ik or in unity editor manually then we set the ik targets on the fps hand skeleton both right and left hands bones should have a child transform as ik target then we'll have two limb iks of final ik set their target and some other settings i won't go too detailed about final ik as they have a lot tutorials and very easy to use then we run the game the characters hands is now controlled by the ik and follow the fps hands we'll alter the position and rotations of the ik target so the character hands should be placed correctly write down the values of their position and oilers and set them back after the run stops we'll also need to adjust the finger bones of the character like the left hand fingers should grab and hold the weapon the same with right hand fingers to grab the grip and hold the gun trigger then we need to add the generic baker from puppet master we add generic baker component to the character's root transform set its mode to real time because we need to bake the animation at runtime like when we play reload we'll bake all transformation data of all bones in the entire hierarchy as a new reload animation then we play shoot we'll bake a new shoot animation however the generic baker cannot be used directly you need to write a custom code to call its function to work like how much time you want to bake what name you want the animation clip to be and when to bake in my own project i create a script that can read all animation states on the fps hands animator and select a state to play and bake it after baking you would have the result animations clips and during baking processing you could have more settings to adjust your character like you probably should rotate your character's chest bone to make your left shoulder forward a little and you might want to animate the bend goals of limp by k during baking as we don't have a default animation to play on the character route you can freely adjust the characters bone if need to have better baking result then after all animations are done we'll delete the default fps hands mesh and use the weapon and our characters hands and save them into one single prefab then as a final process i'll adjust the animation in emotion if there are glitches of meshes then we can use the final result in game the entire workflow might sound like a daunting task but it's definitely doable and once you break through this workflow you're totally unstoppable as now you can use any character and first person animations on asset store to create your own animated fp hands for your game however there are still some cons about the workflow as your character hands are driven by ik the result might not be perfect like the wrist might be twisted in some cases and if you want better result it will still take more time to polish it but it's already a boost for you to create animated fp hands in unity then create them from scratch full body system full body is the character model that will be only seen by the player himself when he looks down it's not like the other player's character model you see in multiplayer games and it's not like the character model in third person games either so i call it full body character as a distinction the full body will be under the character root transform to follow its position and rotation here are the goals of full body system we need the head and arms invisible as you don't want to see a head mesh moving in front of your camera and a full body should cast shadows on the ground including head and arms the full body should match camera height convincingly i mean convincingly doesn't mean exactly if it doesn't look too weird then it's okay camera should avoid clipping into full body like when looking down or play animation full body character should have aim direction correctly with your first person camera even though you can't see your full body's arms and head but they have shadows on the ground so you don't want it to be too weird first let's see the fps mesh tool already provides a shader called invisible shadow caster that can make your head and arms invisible but still cast shadows and will keep cast shadows on on the full body skinned meshes so they could have shadows on the ground then instantiate the full body character model under character root transform the size of full body character can be smaller than the capsule and lower than camera position but when directly looking down your camera will see the open neck of your body so we need to fix it in some games the camera might be at head position when you look to the front but its position isn't exactly at the head position when looking down it can be at front of the full body's chest or at the waist so players can only see the legs or some of the belly some games also would set a limitation angle to stop player from looking down vertically in my project because i want players to see their customization of the character it'd be quite shame if they can't see the clothes so the camera must be able to look directly down so i use a camera offset curve as a workaround to solve the issue by the look down angle the camera will move to an offset forward and downward so when players fully look down vertically the camera's position is at the front of the head in some distance to have a complete view of the upper and lower body and for different locomotions like unarmed rifle pistol locomotions the camera have different look down offsets correspondingly even for different status like running and standing in place the offset would also be different and for the full body animations try not to use any animations that sway the body a lot because i use the code driven path to control the camera the full body has to behave gently without cause glitch so i don't have sprint animations and jump animations in the full body in my game because sprint and jump animation assets swings too much and cause glitch actually i could use ik to snap the full body's head bone to a fixed area but use only running animation already works fine in my case full body character state machines can be similar to a third person character but it could be much less detailed i won't go too much about the implementation but i still recommend to use code to control state switching and i will talk a little about aiming with ik this part can also apply to third person or multiplayer characters we'll create an aim target transform and put it at a far distance away along the camera direction and a full body character's hand would always aim to this target when holding a weapon in final ik plugin i use a biped ik and a mi k for this you can also find how to do aiming in demo scenes of final ik the weapons is under the right hand bone and i used biped ik to match the left hand relatively to the right hand with an offset so the left hand could be placed near gun barrel correctly and if the right hand moves around the weapon and left hand will be moving by the right hand as well for different weapons the values of these settings should also be different then i use a mik for right hand to aim it requires an aim transform as a reference to start aiming which should be near the rear side of the gun and use the aim target transform as aim target which is where the first person camera looks at then when biped ik and aim ik work together you would have a correct aiming result of the head and arms please note that if you're playing animations like reload show hide weapon you could disable the biped and aim ik or have animations work correctly if aim ik is on in call of duty world war ii when you reload your ik is disabled then when reload is done the ik is back on and for better result you could have blend tree of aim poses as an override layer the aiming pose will change by the angle you look right and look down the angle might not be precisely as what it appears to be then you could scale the parameter a little when used this way then let's see the turning animations turning has always be a complicated thing in fps game well in most cases multiplayer character and full body characters are both less detailed than the character in third person games so the turning don't need to look too precisely but fps games have a hard problem that your camera may rotate at very high speed especially with a mouse as input but in third person games you could always give the character angular speed as limit when character turns around so in fps games there are a lot to consider first let's see the basic turning roll in the condition of standing in place will no longer keep the full body character always rotating along with the camera if your camera rotates the full body character will still stands in place no rotation and the aim target or the look target will always be at the direction of where your camera looks at look target could the same like game target when aiming but i'll call if look target here because this part more focused on horizon plane look target is where the camera looked at position projected onto the horizon plane which is the xc plane in unity and we would use it to make character to face the look target so when turning camera full body character will try to look at the look target without changing rotation of itself when player not holding a weapon use look at ik from final ik plugin then let's see when the look target moves around the look at direction will rotate left and right we'll use an angle limit as boundary to determine when we should turn for example the 120 degrees area in front of the full body character when the look direction is inside this angle boundary the full body character stands still but when the look direction is over the boundary it's time for the full body character to turn for turning implementation i use quaternion interpolation of the full body character it will interpolates to the look direction in each update at a slow speed at the same time full body character plays a turning animation at the same time in the blend tree way the blend tree is made of turn left 90 degrees and turn right 90 degrees with a parameter of the turn value which stands for the angle of how much you should turn then the turning is done the basic turning is simple but here's the hard part what if when doing turning behavior my camera is still rotating fast you might need to break the current turning and turn to the opposite direction or when current turning is done keep turning it the same direction on and on this sounds reasonable but take time to make the code tidy with all the mess and what if we keep rotating the camera really fast very fast like you set mouse sensitivity to max in counter strike and shaking the mouse and at this time there's barely no way to determine what direction to turn and when to turn so i make a work around to solve this that'll keep track of the rotation speed of first person camera in each update and use the angular speed of camera to determine how fast full body character should follow the look direction the faster your camera rotates the faster your full body character follows the look direction and if the speed is over a threshold we don't play turn animation at all so you'll see the full body character looks like it's always snapping to the rotation of your camera this might sound confused but i've seen other games behave like this about weapon aim i use the side aim and center of screen aim at the same time and they are interpolated by the ads value which is how much you hold aiming down the site first we have a transform at the position of front sight of the weapon if you use optical sights it should also be at the middle of optical glass when shooting the gun we'll have a shoot direction from the camera to the center of the screen which is actually just the direction of the camera also we create another shoot direction by the position of camera and the position of front sight which is the line is for aiming down sight then interpolate these two result by the ads value when you're fully in hip fire mode the ads value is zero when you hold or switch to ads mode the value goes up in between and when you're fully aiming down sight the ads value is one so the interpolated result would be the final shoot direction we use this direction is where we'll shoot the bullet forward if no bullet spread and for recoil and spread there are plenty ways to make good recoil and bullet spread some games have very classic spread patterns for different weapons so gamers would have nice muscle memories i do these in simple ways so it probably won't apply to your game but it might give you some idea to create recoil i used just a kick and a swing to rotate camera when you fire a weapon the kick value goes up by the weapon setting and a random swing is generated between swing left and right these two values will create an additive rotation for camera it also considers restore strength if the shooting stops the restore will make the additive rotation back to euler zero zero zero for bullet spread i just use a single angle value for the bullet to affect the shoot direction so the bullet will drop circularly around the ideal shoot direction and the strength goes up by how many bullets you just fired and there are multipliers for different player status like for standing crouching running jumping ads they all have different spread multipliers to multiply with the spread angle example if you want when aiming down sight full there should be no spread at all and if you're in hipfire and running the spread should be pretty large and some extra tips about camera rotations even though i use the code driven to control everything i still have an animation driven feature for the first person camera for example when you reload the weapon the camera will rotate by the animation correspondingly so let's see we could find a camera bone inside the first person hands and we'll keep a reference of the camera bone at default the camera's animation driven is off but when it's on we'll calculate its local rotation by the weapon to bone then we'll apply this rotation to the first person camera when you apply the rotations to first person camera it's added additively this also include recoil rotations shaking rotations so it's pretty much everything of the video in short you should make determined designs of the fps player ahead of time even maybe before you create the project go to see how other fps games do don't go to see the a game in recent years because a lot of them might use motion matching or super complicated logics so it's impossible to guess how they do things you could use a global bobbin curve to create bobbing procedurally for first-person hands and use first person view plugin if you don't have a better solution you could use the unity itself to create first person hands as a quick workflow and some tricks about using full body character thanks for watching
Info
Channel: Ryanflees小白
Views: 48,363
Rating: undefined out of 5
Keywords: fps, unity, game development, gdc, tutorial
Id: CmEyXX-7EAc
Channel Id: undefined
Length: 36min 12sec (2172 seconds)
Published: Fri May 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.