Utilities
Animation Behaviours
Animation graph behaviours are attached to states in the unity animator controller graphs. NeoFPS uses 2 behaviours that allow animations to sync the active state of objects to specific clips, and to swap animated objects with dynamic physics objects. These are used in the modular firearms during reload animations but can have a number of uses besides.
For more information, see the AnimStateObjectActivator, AnimStateObjectSwapper, and AnimStateObjectSwapperTarget references.
Pooling
Pooling is an important optimisation in any game. It means pre-instantiating a pool of objects before they are needed that can be spawned and recycled at any time. Instantiation and the garbage collection resulting from object destruction are relatively expensive operations and it is best practice not to instantiate or destroy objects during gameplay. The NeoFPS pooling utilities can specify pool sizes for individual prefabs that will be instantiated at startup. These can then be retrieved and returned as needed.
The PoolManager specifies the pooled objects to be made available across all scenes, along with a number of default settings, while the ScenePoolHandler is responsible for preallocating and accessing pools within the active scene. Retrieve a PooledObject with the following function:
public T GetPooledObject<T> (PooledObject prototype) where T : MonoBehaviour
The PooledObject can be returned to the pool with the following function:
void PooledObject.ReturnToPool ()
If a prototype PooledObject is not managed by the PoolManager already then a new pool of the default size will be instantiated. This is an expensive operation so it is best to try and anticipate the required objects in advance.
Object Lifecycle
NeoFPS uses a number of simple utilities that manage the active state and lifecycle of an object. See the TimedDisabler, ObjectLifecycleManager and ObjectLifecycleMgrDictionary for more information. You can also use a TemporaryPooledObject which has the lifecycle built in for objects that should only be visible for a brief period.
Teleporters
There are 2 teleporter setups provided in NeoFPS:
- The Teleporter behaviour is used in the NeoFPS demos to quickly get around the scenes. It is used for physical teleporter objects that you step into and interact with to teleport to another location. The target location needs to be another teleporter, and that teleporter will be disabled for a bried period on teleporting into it.
- The TeleportZone1Way behaviour is used for simpler navigation around scenes. On stepping into the teleport zone a timer will start and an onEntered UnityEvent will fire that you can use to trigger particle and audio effects. Once the timer completes, the character is teleported to the target transform and an onTeleported event is fired. If the character leaves the zone before the timer completes, then the timer resets and an onCancelled event is fired.
Slow-Motion System
NeoFPS implements a simple slow-motion system that allows you to create slow-mo effects in response to various triggers. The SlowMoSystem behaviour can be added to the player character to create an adrenaline or focus style system as used in games like F.E.A.R. This system has a slow-mo charge which is drained while in slow motion and the recharges when back in normal time. You can also zero the drain or recharge rates for more control over when the effect starts or stops.
To trigger the slow-mo effect, NeoFPS implements a number of sample mechanisms. These include:
- The SetTimeScale Motion Graph Behaviour triggers slow-mo when entering a specific motion state.
- The SlowMoZone behaviour is used to define a trigger zone. When the player character enters, time is slowed. When they exit, time speed is restored.
- The TimedSlowMoZone behaviour is similar, but the slow-mo effect only lasts for a short period (based on the drain rate).
The slow-mo system works by modifying the following script property: NeoFpsTimeScale.timeScale. This is similar to Unity's Time.timeScale, but fires events that allow other systems to monitor the time scale. This allows things like [audio sources][unity-audiosource] to react by modifying their pitch (and therefore speed) via the AudioTimeScalePitchBend behaviour. If you want to create extremely simple slow-mo effects (such as when liberating an outpost in far cry), you can modify this property directly.