HITSCAN vs PROJECTILE | Explanation and Unreal Engine Implementation [UE4/UE5 EA2]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you ever thought about making a game that involves shooting you have probably wondered what the best way of implementation is i'll walk you through an example with unreal engine and show you the differences between hitscan based shooting and projectiles but let's cover the basics first a hitscan weapon will use a line trace or raycast to draw a straight line forward from the muzzle of the weapon or the camera to determine if you hit an enemy there is no travel time and the trace will arrive at the target position the instant it is spawned no matter how far away the target is many source games like half-life 2 and counter-strike mainly use headscan shooting in many cases it will look like there's bullets flying out of the gun however this is only a particle effect and doesn't affect head detection projectile based weapons will spawn an actual projectile in the game world which has a hitbox attached to it and moves forward at a set speed it has travel time and depending on how far away your target is it might take a few milliseconds to seconds for the projectile to hit the projectile could be said to be influenced by gravity wind and other factors as well but this is optional an example for this is the battlefield series you need to anticipate where your enemy will be by the time your bullets hit and lead your shots accordingly many games choose to implement both hitscan and projectile based weapons together rocket launchers and grenade launchers for example will always be projectile based in multiplayer games that have large maps with wide open areas snipers are usually also projectile based because otherwise they would be too dominant the best example using both methods that i'm familiar with is overwatch for example soldier's left click is a hit scan shot that will hit instantly while his right click is a projectile with travel time for torbjorn both his left click and right click are projectile based the left click is a single bullet affected by gravity while the right click shoots multiple bullets in a spread pattern reaper also uses a shotgun for his left click however his weapon is hit scan and shoots out multiple line traces in a spread pattern if you're playing dps and facing off against the pharah player you will quickly understand the difference between hit scan and projectiles it is much easier to hit a target that is far away and moving around in hard to break patterns without having to take the speed of your bullet or gravity into account a great projectile player might still be able to take her down but a hitscan based character like soldier or ash will be able to do so much more consistently depending on the kind of game you're making you need to decide on what method you want to use or just build a system that allows you to easily incorporate both let's look at the differences again hitscan uses line trace to determine collision hits instantly feels snappy but unrealistic easy to implement in most game engines simple calculations that aren't resource intensive projectile uses attached collision sphere has travel time of milliseconds to seconds feels more realistic harder to implement but has built-in support in unreal engine more resource intensive because of spawning collision checks and movement every bullet needs to be updated every frame so this can get quite heavy when working on our indie game monster showdown i went through a lot of trial and error regarding the shooting mechanics since we wanted to have crossbows and rocket launchers we definitely needed to support projectiles and made all weapons use projectiles at first however in our case making snipers and revolvers hit scan based made the shooting feel a lot more satisfying and also allowed us to implement shot penetration much easier so we ended up having both systems in the game let's get started with implementation by the end of the video we'll have a system that allows us to easily switch between shooting projectile weapons and hitscan weapons i'll be working with unreal engine 5 today if you're only interested in the concepts just keep watching the video but if you want to code along feel free to do so everything we'll do today works the same way in unreal engine 4 so you don't have to use unreal engine 5. once loading is finished we'll select the games tab and the first person character project this is going to be a blueprint project with starter content enabled for some effects once your editor is loaded up it should look like this we have a first person character that can already shoot projectiles move around and jump we can see that the projectiles interact with the white boxes and add an impulse and then disappear if we hit the wall we can see the projectiles bounce off of it we also see the projectiles affected by gravity and falls down slowly [Music] open up the blueprint of the first person character [Music] the first thing we see is the fire event when the left mouse button is clicked this will fire off and shoot our bullet when we scroll up we can see the movement mechanics jumping but also a lot of stuff that is only related to vr so we can just delete this or ignore it [Music] in our fire event there are also things that are only vr related so we have to delete them for the sake of clarity [Music] these notes play our character animation when we shoot we want to keep them regardless of if it's hit scan or projectile so we move them to the left these nodes play the fire sound we also want to fire this off regardless of if it's hit scan or projectile we can now also delete these two variables since they are only related to vr before we take a closer look at our projectile spawning we are going to select all of these nodes right click and collapse to function this way we can keep our blueprints cleaner [Music] let's have a closer look inside our shoe projectile function this node is not needed anymore so we can also delete it and connect our nodes again let's clean this up a bit [Music] when the shoot projectile function is triggered we spawn a new actor of the type first person projectile if we click here we can see it in the editor we also need to give it a spawn transform this consists of the location rotation and scale the scale will just be kept to one we can get the location from the world location of our sphere the sphere is attached to the muzzle of the weapon however we also have a variable called gun offset which will help offset the shooting position to align with the crosshair we get the rotation from the world rotation of our camera because you always want to shoot the direction we are looking at we can also delete these four components since they are only related to vr let's look at our first person projectile we can see that it is rather simple and only has three components the first is the collision component which is a sphere in this case we can check the collision settings and see that it is a custom preset for this project it is important that we have blocking set for world static and physics body the sphere part is a static mesh that has no collision and is only there for visual representation the last but most important part is the projectile movement component this is a pre-made system with unreal engine that allows us to easily make bullets set their speed how much they are affected by gravity if they should bounce or many other properties without having to code this ourselves let's edit a few properties and see what changes the bullet is now much faster and will stick to walls rather than bounce off in your project you can play around with these values to reach the desired result open up the first person character blueprint again add a boolean that allows us to set if we want to use hit scan or not only changing this one value will later allow us to switch between both systems easily [Music] implement a branch after we play our shot sound and in between the shoot projectile function if hitscan is set to false we will shoot our projector like we have been so far create a new function for hit scan shooting and place it after our branch in the true case open up the function for collision detection we now want to create a line trace there are many different ways to create line traces but we will pick a line trace for objects the first parameter we have to pass to this node is the start position of the line trace we want to start drawing a trace from our first person camera position so we grab it and get its world location then connect it to the line trace node you might think it's strange to draw the line from the camera not the weapon but this is very common practice in shooter games and allows us to be very precise with our shooting getting the end location is a bit more complicated first we need to get a direction we can get the direction we are looking at by getting world rotation of the camera and the forward vector create a new variable with the range of our shots which is the float i will then compile and set this to twenty thousand our trace will move forward 20 000 units we then get the forward vector which indicates direction and multiplied by the shot range [Music] since the endpoint depends on the starting position we have to add our starting position here then connect the nodes to our line trace we also need to pass which object types we want this trace to interact with so we make a new array and set the object types that we want in our case we want to be able to hit the walls so we use world static and also the white boxes so we use physics body when you go to the map you can check the white cubes and see that in their collision settings they are set to physics actor preset and object type of physics body for debugging purposes we will set draw debug type to for duration and set a draw time this will show a line where our line traces [Music] if we shoot now we can see a line and a dot where it hits the walls or the white boxes so we can already tell it's detecting the types we set however there are still two issues the white cubes don't receive an impulse and our line trace is not aligned with the crosshair let's open up first person hud they are doing a couple of things here that are kind of strange to align the crosshair with the projectile like adding an offset of 20 here so we're going to make a system that allows us to keep what they did and also add a different branch for projectile screen w and screen h set the width and height of the crosshair we are going to turn them into variables so we can easily change the values for these let's compile and set them to 32 so they are more visible [Music] we then add a boolean here studying if we're using hit scan or not just like we did on the weapon in a real project you would probably put this on gamemode and pass it down but for the sake of not getting off topic too much we are just going to use this dirty implementation set aside scan to true and drag it onto our blueprint we only want to apply the offset of 20 if we are not using hitscan so create a select node here and use this hitscan as the index in the false case we want to use the offset of 20 in the true case we use zero our line trace is now closer to the crosshair but it's still not perfect the crosshair is still off to the right and down add a subtract note to the horizontal line also add a subtract note for vertical in the case that we are using hit scan we want to offset the crosshair by half of its size so add a select node for the horizontal and for the vertical line here set the false case to zero because we don't want to change anything and for the true case get screen w and divide it by 2. pass this to true for horizontal we do the same thing using screen h [Music] the line trace is now perfectly aligned to the crosshair we now want to add the impulse to our wide cubes so go back to first person character and find the place where responder line trace we check the boolean of the return value and only keep on processing if this is true if this is false it means we didn't hit anything break the hit result which will give us much information about what we hid and where we impacted it we want to look at how the impulse is handled in the first person bullet so we go over to its event graph and copy these notes over [Music] paste them in here and connect them to the true case we need to pass the hit component to add impulse at location we then check if the hit actor is simulating physics or not in case it is not simulating physics we won't add an impulse the next thing we need to prepare is the impulse itself for this we need a direction and a strength which will then be turned into a vector the direction will be the forward vector of a first person camera we create a float variable with the impulse strength and set it to 200 thousand [Music] we then multiply our forward vector with the impulse strength and connect it to the impulse node on add impulse at location [Music] the last thing we need is the location of the impulse we can simply get this from our hit result if our line trace hits a white cube now it will add an impulse to make it more obvious or a line trace hit we want to spawn a particle effect add the node's spawn emitter at location we can use particle effects here that came with the starter content [Music] as the emitter template we pick p explosion and also connect the location from the hitch result [Music] we don't want to draw a debug type anymore so select none now we have a hit scan effect like most shooter games where you don't see a line but you only see a particle effect at the impact point this concludes the video i hope you got some valuable information out of this and see you on the next one
Info
Channel: Cobra Code
Views: 16,624
Rating: undefined out of 5
Keywords: unreal engine, unreal engine 5, unreal engine 5 tutorial, hitscan, projectile, hitscan vs projectile, hitscan vs projectile unreal engine, hitscan vs projectile ue, hitscan vs projectile ue4, hitscan ue4, hitscan ue5, projectile ue4, projectile ue5, ue4 shooting, ue5 shooting, ue4 game development, ue4 shooting tutorial, ue4 fps tutorial, ue4 fps, fps game development, fps beginner tutorial, fps basics, unreal engine basics tutorial, game development, how to
Id: pWIJlsqli1w
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Sat Oct 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.