First-Person Controller

A basic character controller based on Unity's Character Controller

The first-person controller in the FPS Framework is based on the character controller, which gives a lot of control over the character without any external physics behaviors.

Creating a first person controller

The FPS Framework has a lot of creators; these creators are responsible for creating things like Weapons, characters, and UI. To access the FPS creator, right-click on the hierarchy. This will show a menu. Click on the menu Akila, FPS Framework, then FPS Controller, like this:

Creating a new FPS Controller

After clicking on FPS Controller, you should have a functional first-person controller in your scene. The character has a camera by default; if your scene already has a camera, delete it.

Now this is what you should see:

Created a new FPS Controller

Fields

Name
Description

Acceleration

The start and stop movement speed. The higher the value is the slower the character will start moving and stop.

Walk speed

The speed of the character while walking. The higher the value is the faster the character will move.

Sprint speed

The speed of the character while sprinting.The higher the value is the faster the character will move.

Tactical sprint speed

The speed of the character while sprinting tactical sprint.The higher the value is the faster the character will move

Jump height

How high the character can jump. The higher the value is the higher the jump is.

Crouch height

How low the character can crouch. The higher value is the less the character will lower itself.

Step interval

How fast are the character steps (This doesn't affect movement speed but the sounds). The higher the value is the faster the character will step his steps.

Slide down slopes

If true the character will slide down when on slope with a height angle than CharacterController.slopeAngle.

Slope slide speed

How fast the character will accelerate on slopes. The higher the value is the faster the slide will be.

Gravity

A multiplier for the gravity of the character. This is Physics.gravity * gravity

Max fall speed

Max falling speed in Km/h

Stick to ground force

The force applied to the character when on ground or slope, the higher the value the lower jumping is and

Camera

The camera or camera holder that the character should move when rotating view.

Sensitivity

How sensitive the view rotation is to the input.

Maximum X

Max angle that the view can reach.

Minimum X

Min angle that the view can reach.

Dynamic sensitivity

If true the sensitivity will change with the field of view and will keep the same feeling.

Lock cursor

If true the cursor will be invisible and locked on start

Global orientation

If true the orientation will be global meaning the rotation of the character itself won't affect the character view.

Footsteps SFX

A list of audio profiles that plays when a step happens.

Jump SFX

An audio profile that plays when jumping

Land SFX

An audio profile that plays when landing

Events

Name
Description

On jump

Gets invoked when jumping

On land

Gets invoked when landing

Adding sounds

The only thing missing now is the audio. To add audio, click on the first-person controller object you just created and assign the sound to its fields. The sounds are not audio clips but audio profiles, but what's an audio profile?

The framework has a dedicated audio system that manages playing sounds better than the default methods, and this audio system uses audio profiles to do its work. If you want to create an audio profile and assign it, right-click on the project window and hover your mouse cursor over the menu Create, then click on the item Audio Profile like this:

Creating an audio profile

Now assign the audio clip inside of your new Audio Profile and assign the new audio profile to the appropriate sound effect field in the First Person Controller component.

For more information about the audio system, see this.

Supporting custom controllers

The ICharacterController uses an interface to communicate with other systems. By implementing this interface, integration should work automatically.

Understanding how the system works in a unified environment is essential. Therefore, it’s highly recommended to use Unity’s Standard Assets FPS Controller while following along, ensuring no external factors affect the integration. You can download and import the .unitypackage from this link.

The integration is handled through several components, including the Character Manager, Input Manager, and an interface called ICharacterController. Let’s begin by adding these components to your custom character controller object, as shown below:

Use Akila.FPSFramework namespace to get access to all FPSF's code base.

using Akila.FPSFramework;

public class FPSController : MonoBehaviour

Implement ICharacterController interface as shown below.

using Akila.FPSFramework;

public class FPSController : MonoBehaviour, ICharacterController

As far as this, FPSF knows about your custom movement, but it's unable to change your player speed, or invoke the events in CharacterManager class and other things like this. To fix these, the CharacterManager needs to be feed with some info in the Update() function.

In your script, refrence the CharacterManager and call SetValues() function as shown below.

using Akila.FPSFramework;
using UnityEngine;

public class FPSController : MonoBehaviour, ICharacterController
{
    public CharacterManager characterManager;

    // Your own custom fields
    public bool isGrounded;
    public float walkSpeed;
    public float sprintSpeed;
    public Vector3 playerVelocity;

    // A value used in your custom movement class that multiplies the total velocity.
    // This value is copied from the CharacterManager to modify your custom movement speeds.
    public float speedMultiplier;

    private void Update()
    {
        // Notify the CharacterManager of the current values.
        // It can then trigger events and update other scripts accordingly.
        characterManager.SetValues(playerVelocity, isGrounded, walkSpeed, sprintSpeed);
    
        //Copy the value from CharacterManager since other classes change this value
        // This float is used to modify the movement speed for example, when aiming.
        speedMultiplier = characterManager.speedMultiplier;
    }
}

The scripting is done, only thing missing now is to use FPSF's built-in components with your custom FPSController. To speed this up, create a new FPS Controller and drag the entire Camera object from the new FPS Controller to your custom FPS Controller like this:

The custom FPS Controller with the entire Camera object

Last updated

Was this helpful?