Week 2


Week 2 Player shooting (Raycasting)

https://www.youtube.com/watch?v=THnivyG0Mvo - TUT used

 

Within the gun script I created a public float to determine both the damage and the range of the gun, also am using input “Fire1” to shoot the gun. Recasting is when we shoot out an invisible ray from the position of the camera and in a forward direction were facing, when the ray hits something we can gather that information and use it to determine what happens, so when we hit a target we can damage it and when we miss we don’t damage anything. To shoot from the camera I created a reference to it with a public camera (named fpsCam). Within the shoot method I created a RaycastHit ((hit) a variable used to store information about what were hitting with the ray). To shoot out the ray I used Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range), what this does is shoot out the ray from the position of the camera and in the direction the player is facing, it also puts all the information we need into the ‘hit’ variable and uses the guns range (in this case anything 100 units away aren’t going to be able to be shot), this is all using within an IF statement so that it only triggers if we hit something. 

Leave a comment

Log in with itch.io to leave a comment.