How to add shooting to first-person controls in Unity

Let’s go over how to add shooting using raycast to the first-personcontrols.

I showed how to set up the movement and camera controls in this article.

Start by creating the particle effect for where the “bullet” hits or download a suitable one from the asset store. Turn the particle effect into a prefab:

For the cross-hair add a UI image to the scene and position it in the centre of the screen:

Create a script called PlayerShoot and add to the player object:

Open up the script and add a serialized GameObject for the bullet spark prefab:

Create the Shoot function. Add a Vector3 with the value 0.5 on X and Y which will be the centre of the screen when passed into ViewportPointToRay. If the ray hits an object instantiate the bullet spark at that location. Destroy the prefab after 1 second:

Lastly, call the Shoot function from Update when the left mouse button is push down:

Good luck!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store