Introduction
A lot of endless runner games allow the player to grab objects along the way. From coins to health bonuses or weapons, it’s really one of the essential features of most games. The Infinite Runner Engines contains everything you need to add pickable objects to your game.
The Base Class : PickableObject
This class is meant to be extended, don’t add it directly to one of your prefabs, it wouldn’t do much. However, it’s important to know it exists as it contains all the base logic required by all pickable objects :
- collision detection with PlayableCharacters
- on collision, instantiates pickup visual and sound effects (specified in the inspector), disables the object and calls ObjectPicked
- on ObjectPicked, does whatever you tell it to
So mostly to create your own pickable object you just have to extend PickableObject into a new class, and override ObjectPicked
Examples
The Infinite Runner Engine contains a few examples of pickable objects :
SpeedBonus:
public class SpeedBonus : PickableObject
{
public float SpeedFactor=2f;
public float EffectDuration=5f;
protected override void ObjectPicked()
{
if (LevelManager.Instance == null)
{
return;
}
LevelManager.Instance.TemporarilyMultiplySpeed(SpeedFactor,EffectDuration);
}
}
This speed bonus, used in the SkyTheory demo scene, allows you to define a speed factor and effect duration from its inspector, and calls a method in LevelManager when picked.
Coin:
public class Coin : PickableObject
{
/// The amount of points to add when collected
public int PointsToAdd = 10;
protected override void ObjectPicked()
{
// We pass the specified amount of points to the game manager
GameManager.Instance.AddPoints(PointsToAdd);
}
}
This simple coin component asks the GameManager to add points (specified in its inspector) when picked.