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!

--

--

--

Unity / C# Game developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cup 7 Group

A Brief Intro To Cookies

Golang Unix time

How to Change Login Screen Background in gnome. Ubuntu 20.04/Ubuntu based distro!

Amplify Your IT Career by Building Infrastructure Development Skills

An Overview of Docker Architecture

Docker engine image

What Does It Take to Ensure Quality and Accuracy in BIM?

The Caesar Shift Cypher in Python

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
Niklas Bergstrand

Niklas Bergstrand

Unity / C# Game developer

More from Medium

Is 2022 The Year For Consumer AR Glasses?

Education As A Dapp

CS373 Spring 2022: Matthew Escobar Blog #3: Impatience is a Virtue

Share Your Media Layoff Experience