Raycast for FPS shooting in Unity 3D
Jun 23, 2021
Using raycast is an effective way of calculating hits for guns.
To do that we need to start our ray from a point on the screen, we are using slightly off centre for our simulation, so we make ourselves a variable to store this vector. we also make ourselves a variable to store our ray and rayhit.
Then using an if statement we check if our raycast hits anything of value for a response, in this case anything with an IDamagable inheritance script. if we find it we are dealing damage, and instantiating a bloodsplatter effect at the point of hit, then starting a corutine which will stop us shooting for a short period of time.