Introduction To UFPS: Unity FPS Tutorial
In this Unity FPS tutorial you’ll learn the basics of working with UFPS to build the basis for a solid first person shooter in Unity3D. By Anthony Uccello.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Introduction To UFPS: Unity FPS Tutorial
35 mins
vp_FPWeaponShooter
Expand the Projectile section under the vp_FPWeaponShooter in the Inspector. This controls the firing of the gun.
-
Firing Rate
is how long the gun animation fires. -
Tap Firing
is an override that lets the player fire without waiting for the fire animation to finish — it resets the fire animation. -
Prefab
is the bullet. Keep in mind that bullets have their own script undervp_BulletFX
, and you can learn more about it in the documentation.
Expand the Muzzle Flash section. You can configure the art prefab as well as its scale and position to achieve the perfect flash effect each time the player fires the weapon.
Expand the Shell section to see the settings that control what happens when the shell ejects from the gun.
Finally, expand the Sound section. Here you configure the settings for the sound the weapon makes when the gun fires with and without ammo.
You’ll also notice a States
section in here too. It is similar to the state section for the vp_FPWeapon. If you’re curious how this works you can look into the source code, which involves the advanced topic of Reflection — you’ll see a link at the end of this tutorial where you can learn more.
Adding a Shotgun
What’s the point of building an FPS if you’re not going to give your hero a tool that destroys enemies at short range? There isn’t. It would be a sad, sad little FPS without such a weapon.
To allow you to stay focused on UFPS, you’ll just add the gun model and won’t mess around with hands.
In the Project browser, type 2Shotgun and drag the 2Shotgun prefab under the 1Pistol GameObject in the Hierachy.
You might notice that the vp_WeaponShooter
doesn’t have any options, and that’s because the prefab is disabled. Toggle the active box on the 2Shotgun GameObject.
You might be thinking you can just click play then press 2 and load the shotgun right? Well — almost! The SimplePlayer
prefab is only configured for the pistol.
To make it work for new weapons you need to add a special component. Click the SimplePlayer GameObject in the Hierachy and then click Add Component in the Inspector. Type in vp_FPWeaponHandler and select the script. This is the component that allows weapon switching.
Set the value of Start Weapon to 1 to set it to the pistol. Now when you click Play and then press 2 you’ll swap your pistol for the shotgun.
Wait a minute…something is off — you’ve got that gun in a rather precarious position! With the 2Shotgun prefab selected in the Hierachy, expand the PositionSprings section of the vp_FPWeapon
in the Inspector and set the Offset to (X = -0.04, Y = -0.46, Z = 4.89). Expand the RotationSprings section and set the offset to (X = -1.09, Y = -93.3, z = 2.5).
The shotgun should fire a cluster of bullets. Where’s the fun in a single bullet?
Expand Projectile on the vp_FPWeaponShooter and set Count to 10 and the Spread to 2.
Expand the MuzzleFlash and set the prefab to MuzzleFlashShotgun.
Set the Position to (X = -0.08, Y = -0.24, Z = 8.88) and set the Scale to (X = 1, Y = 1, Z = 1). If you want to figure out these values yourself just edit the MuzzleFlash Position
during gameplay and update the position after you’ve stopped playing — the muzzle flash stays on so you can precisely position it.
Expand the Sound section and set the Fire to ShotgunFire.
Click Play then press 2 and add some holes to the target!
Zoom is a special state pre-configured for the RMB (right mouse button) in UFPS. Upon clicking, it sets the zoom state for all components that are set to listen for this change.
You can add custom zoom states. Try it for yourself by clicking Play and pressing 2 to equip the shotgun. Change the Position Springs and Rotation Springs Offset values for vp_FPWeapon so the camera looks down the barrel. Click the Save button at the bottom of the vp_FPWeapon
, name the file ShotgunZoom and click Save again.
Note: If you’re having trouble getting zoom to look right, or you’re on a Mac and can’t see the save button in the vp_FileDialog window, just download this ShotgunZoom and add it to your Assets folder in Unity.
Note: If you’re having trouble getting zoom to look right, or you’re on a Mac and can’t see the save button in the vp_FileDialog window, just download this ShotgunZoom and add it to your Assets folder in Unity.
Now you have the zoom, but the shotgun doesn’t know how to use it. Stop game play and click the 2Shotgun GameObject in the Hierachy. Expand the vp_FPWeapon in the Inspector and expand the States section. Drag ShotgunZoom over to the Zoom field.
UFPS takes the start and end values then creates an animation. Click Play and Zoom with the shotgun and it will animate to where you saved it to for the zoom state.
Adding A Sniper Rifle
Any good FPS game comes with an array of weapons so the player can wipe out enemies in a variety of situations. For those times when you want to pick ’em one by one from a safe distance, there’s nothing better than the almighty sniper rifle!
Type 3Sniper in the Project browser search and drag the 3Sniper prefab under the 2Shotgun GameObject in the Hierachy. In the Inspector, add the vp_FPWeapon and vp_WeaponShooter scripts as you’ve done previously.
Add the sniper rifle model to the Rendering
section of the vp_FPWeapon
by going to the Project browser and expanding the Assets folder then the Prefabs folder.
Click the 3Sniper GameObject in the Hierarchy, and then expand the vp_FPWeapon component in the Inspector. Expand the Rendering section to display the 1st Person Weapon (Prefab) field. Finally, drag the m24 Prefab from the Project browser to the 1st Person Weapon (Prefab) field.
Under vp_FPWeapon
, set Position Springs to (X = 1.4, Y = -0.81, Z = 4.88) and Rotation Springs to (X = -2.7, Y = 79.62, Z = -5.01). Expand the Sound section on the vp_FPWeaponShooter and set Fire to SniperShot.
The gun is now in a functional state. Click Play and press 3 to see for yourself. Don’t forget to give it a test fire. :]
“Functional state” is rather subjective, don’t you think? There are no crosshairs when you zoom! Adding a scope zoom is an excellent use case for creating states while playing the game.
It’s time to add a Sniper component that helps with zooming. This is a special script that needs to listen for player events in order to work.
Open the Scripts folder, right-click anywhere and use the pop-up menu to create a new C# script named Sniper. Double-click to open it and replace its contents with the following code:
using UnityEngine;
using System.Collections;
public class Sniper : MonoBehaviour
{
//1
public vp_FPPlayerEventHandler playerEventHandler;
public Camera mainCamera;
private bool isZooming = false;
private bool hasZoomed = false;
//2
void Update()
{
if (playerEventHandler.Zoom.Active && !isZooming)
{
isZooming = true;
StartCoroutine("ZoomSniper");
}
else if (!playerEventHandler.Zoom.Active)
{
isZooming = false;
hasZoomed = false;
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(true);
}
if (hasZoomed)
{
mainCamera.fieldOfView = 6;
}
}
//3
IEnumerator ZoomSniper()
{
yield return new WaitForSeconds(0.40f);
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(false);
hasZoomed = true;
}
}
Stepping through the script:
1. These variables keep track of references to the PlayerEventHandler
and the camera. The Booleans are flags that track the state of the zoom.
2. This section checks if the zoom state is active. When the gun is zoomed, it sets the field of view to 6, which is what triggers zoom.
3. This coroutine tracks when the zoom animation will complete — after 0.4 seconds in this case — and then hides the weapon model. This creates a bit of time for the player to look down the scope before transitioning to the scope effect.
Save the file and return to Unity. Drag the Sniper script to the 3Sniper GameObject.
Drag the SimplePlayer prefab over the PlayerEventHandler on the Sniper component. Next, drag the FPSCamera over the Main Camera field on the Sniper component.
In the Hierarchy, you’ll find a UI GameObject that has been set up with a SniperZoom texture.
Go to the Scripts folder and create another new C# script. Name it GameUI. Select the UI GameObject in the Hierachy, and then drag the GameUI script from the Project browser to the Inspector.
Open the GameUI script and replace the code with the following:
using UnityEngine;
using UnityEngine.UI;
public class GameUI : MonoBehaviour
{
public GameObject sniperZoom;
public vp_PlayerEventHandler playerEventHandler;
public void ShowSniperZoom()
{
sniperZoom.SetActive(true);
sniperZoom.GetComponent<Image>().enabled = true;
}
public void HideSniperZoom()
{
sniperZoom.SetActive(false);
sniperZoom.GetComponent<Image>().enabled = false;
}
}
These are public methods that you’ll use to show and hide the sniper scope texture — they keep a reference to the scope texture and the event handler. The Sniper script will call these after you configure it to do so. Save the script and return to Unity.
Expand the UI GameObject in the Hierarchy. Drag SniperZoom to the SniperZoom field in the Inspector then drag SimplePlayer to the PlayerEventHandler field.
Open the Sniper script. Add this variable to the top:
public GameUI gameUI;
Now the script will keep a reference to the GameUI.
Below the else if (!playerEventHandler.Zoom.Active)
statement, add the following:
gameUI.HideSniperZoom();
Before the change:
else if (!playerEventHandler.Zoom.Active)
{
isZooming = false;
hasZoomed = false;
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(true);
}
After making the change:
else if (!playerEventHandler.Zoom.Active)
{
gameUI.HideSniperZoom();
isZooming = false;
hasZoomed = false;
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(true);
}
Now the sniper zoom texture will hide when in the non-zoom state.
Below GetComponent<vp_FPWeapon>().WeaponModel.SetActive(false);
add the following:
gameUI.ShowSniperZoom();
Before the change you should have:
IEnumerator ZoomSniper()
{
yield return new WaitForSeconds(0.40f);
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(false);
hasZoomed = true;
}
After this change you should now have:
IEnumerator ZoomSniper()
{
yield return new WaitForSeconds(0.40f);
GetComponent<vp_FPWeapon>().WeaponModel.SetActive(false);
gameUI.ShowSniperZoom();
hasZoomed = true;
}
This line shows the sniper scope texture when the sniper rifle is in zoom. Save and return to Unity. Select the the 3Sniper GameObject then drag the UI GameObject to the GameUI field.
All of that was to hide the gun and show the sniper scope when the player zooms. Take it for a test run by clicking Play, pressing 3 to equip the sniper and pressing the RMB to zoom.
It works but it’s a rudimentary implementation. You can make it better by setting it so that the player glimpses down the scope when zooming.
Click Play and press 3 to equip the sniper rifle. Expand the Position Springs and Rotation Springs for the 3Sniper and tinker with the position and rotation offset values until the scope is front of the camera.
Click the Save button at the bottom of the vp_FPWeapon component. Name the file SniperZoom then click Save again.
You’ve just finished setting the sniper rifle’s state when it’s zoomed. In this case, you moved the gun up and in front of the camera so it will appear as though the player is looking down the scope, and SniperZoom is where you saved the state.
As of this moment, you’ve got the settings for the state but still need to create the required state. Select 3Sniper and expand the vp_FPWeapon then expand the State section.
Click the Add State button and replace the word Untitled with Zoom in the new State field.
Drag the SniperZoom.txt file from the Assets folder to the Text Asset field of 3Sniper’s vp_FPWeapon
component.