Realistic Sniper Shooting: Ricochet, Decals & Wall Penetration - Unity Tutorial #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is the third part of the sniper shooting tutorial Series in this video I will show you how to make the bullet Ricochet penetrate the walls and leave deals on impact first let's create structure heat info so we can pass more information about the bullet heat to the shootable object this track will contain rast heat Vector with direction of the heat heat speed reference to the bullet object and bullion value that will indicate if the bullet should be destroyed after the impact now let's modify the onheat method so it will have the information about heat vector and we will replace get component method with get components method so we can access not only one but all shootable object components that are on the game object we have heat we have to create heat info object assign all the information about the Heat and in the for each Loop we will pass this info to all of the shootable objects calling on heit method in the and we will check if destroy bullet variable has been assigned to true and if so we will destroy the bullet object now we have to pass property heat Vector to our onheat method which is Vector between current bullet position and bullet position in the previous time step we have to also modify shootable object abstract class to accept heat info as on heat method parameter instead of rast heat object we can see that now there are some errors showing in the unity console window we can select each of this error and fix implementations of classes that inherits from shootable object class to also use reference to heat info trct inside enemy heat script we can now use heat speed and heat Direction variables to correctly apply forces to the rag doll of the enemies we have hit and we can make a parameter that will specify if we want to destroy the bullet object on the [Music] impact similarly we have to modify other scripts that inherits from shootable object class now all the warning have disappeared and everything works fine except that now when we shoot our opponent he explodes into space we will get to that in the moment but now let's also test if we can disable destroying bullets on the heat with the walls Let's test it we can see that particles are instantiated on the impact with the wall but the bullet is flying right through it and then hits the ground but still we haven't solved the flying enemy issue so why this happens well last time we assigned in the enemy hit script value of the impact Force variable to 1,000 and now when we are taking into account bullet speed which is also about 1,000 the impact force is too big we should change its value to one but we have this enemy hit component in every game object of the enemy that have a collider so this is a lot of places to modify and we can miss some so let's create the script that we find each enemy hit component in child game objects and assign correct values to each [Music] one we will mark this script to be executed in the edit mode and defines to make update function only be executed in unity editor in the start we will get every enemy hit component in the object [Music] children's and then in the loop we will assign all the values correctly to each enemy heat [Music] component [Music] now let's assign proper values to the enemy manager [Music] script we can see that enemy heat components have been modified properly let's check if this fixes our [Music] issue now enemy isn't flying anymore to the outer space so we can assume that the issue has been fixed now we can allow the bullet to fly through the wall without being destroyed but still we cannot change its speed or flight Direction so let's make a method reinitialize inside the parabolic bullet class that will allow us to reset bullet start position rotation and speed we have to reassign bullet start position start forward Vector bullet speed and also reset start time and is initialized bullan value now let's modify wall hit speed so it can absorb some of the bullet speed but still allow bullet to fly to the other side we will create speed absorption parameter and calculate new bullet speed if the calculated speed will be smaller than zero or the always destroy bullet variable will be set to true the bullet should be destroyed but otherwise we will reinitialize the bullet with the position of the heat moved by a small offset into the wall so it won't hit the same wall again start forward direction will be set the same as the heat direction and speed will be set to the new speed we have calculated it let's test [Music] it to visualize this better I will increase the absorption value and make the wall transparent so we can see how far the bullet will [Music] fly we can see that bullet now flies much closer if we hit the wall now let's separate particles sping logic from wall hit logic so we can have more flexible system we will create particles hit script and copy the code that is responsible for spawning particles from the wall hit script into the new one also let's extract particle lifetime to parameter so we can easily modify it in the editor we will also remove from wall heat the code responsible for spawning the particles now let's assign to every wall particles hit component and test it we can see that nothing has changed but now we can make spawning particles logic independent from wall heat logic which will be handy later when doing Ricochet heat logic but first let's make theal heat script that will Spa Sprites on the bullet impact we need parameters like dial object which will be instantiated on the heat Max and minan scale and lifetime now we can instantiate deal object in the hit point with a slight offet to the outside of the wall so it won't clip to the [Music] wall we can set random rotation around the wall normal vector and set a random scale between mean and Max specified in the [Music] parameters and destroy the C after specified lifetime I have prepared a simple glass decal for it to work as a Sprite we have to change the texture type to Sprite and select Alpha's transparency checkbox now we can drag the Sprite into our scene and create a prep up out of [Music] it now when we have created a deal prefab let's assign it to the deal heat script and test if it works we can see that it works perfectly and SPS deals with random rotation and [Music] scale for me they are even too large so I will decrease a little bit Max and minan [Music] scale perfect in the end let's make a Ricochet heat script it will have parameters like mean Speed Max angle and Max Speed absorption we will calculate the speed absorption based on the angle between the wall surface and bullet heat Direction Vector to calculate this angle we can calculate the dot product of the wall normal vector and heat Direction Vector because both of these vectors have a magnitude of one the value of the dot product will be equal to the cinus of the angle between them to transform it into an angle we can use arcus cinus and we will convert angle from radians to more readable for us degrees and lastly we subtract this angle from 90° because we want the angle between heat Direction and surface and not the normal Vector for now we will linearly Interpol between Max Speed absorption value when the angle is equal Max angle and zero when the angle is near to zero the new speed will be calculated just by subtracting speed absorption from the heat speed if calculated angle is greater than the max angle or speed is smaller than zero then we will destroy the [Music] bullet [Music] otherwise we will reinitialize it with the new speed and forward Vector that we can calculate using built-in function Vector 3 reflect and passing as the arguments heat Direction and wall [Music] normal now let's attach this script to another wall and test if it works [Music] we can see that indeed this works but let's further improve this script the bullet speed on the Ricochet doesn't decrease linearly with the angle but rather quadratically we can use animation curves to specify in the editor how should this curve look like and adjust it to our likings [Music] now we can use method evaluate to get value of the curve in the specific point and multiply it by absorption multiplier value let's assign proper curve for our Ricochet script the smaller the angle the less we want the absorption to be now we can see that our bullets are recoating really close when we have a large angle of shooting and really far when the angle is small you can experiment with the curves and values and and get the results you like I hope this tutorial will help you develop your own awesome games and that you learn something useful thanks for watching and happy coding cheers
Info
Channel: Shaded Technology
Views: 539
Rating: undefined out of 5
Keywords: unity, sniper, shooting, tutorial, realistic, low poly, ray, casting, ray casting, ray cast, game, scope, parabola, wind, physics, ricochet, decals
Id: bdf0by1ZIb0
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Thu Mar 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.