Creating a new unit Ability
RTS Starter Kit contains an API for ability creation. You can make your own abilities for units with custom logic. You need to create a new class inherited from the Ability class. After implement all needed methods with override directive like this:
using InsaneSystems.RTSStarterKit.Controls;
using UnityEngine;
namespace InsaneSystems.RTSStarterKit.Abilities
{
// Built-in ability code as example
[CreateAssetMenu(fileName = "CarryOut", menuName = Storage.AssetName + "/Abilities/Carry Out")]
public class CarryOut : Ability
{
protected override void StartUseAction()
{
if (Selection.SelectedUnits.Count == 0)
return;
for (int i = 0; i < Selection.SelectedUnits.Count; i++)
{
var carryModule = Selection.SelectedUnits[i].GetModule<CarryModule>();
if (carryModule)
carryModule.ExitAllUnits();
}
}
}
}
You have 3 methods to override: InitAction - being called once when the unit spawns and ability initializes.
StartUseAction - this being called when the player clicks the ability icon and it is possible to use it (ability icon active etc).
UpdateAction - being called every frame with the usual MonoBehaviour Update cycle (from the AbilitiesModule of specific unit).
Don’t forget to add a custom CreateAssetMenu, which will allow you to create a data-file of your ability. Abilities API is very “raw” and doesn’t contain a lot of useful functions like reloading, targeting etc for the 1.6.6 version. You can try to implement it yourself, or wait for our updates.
If you need to pass to your ability some data from the unit owning it, for example, child transform or something, you need to create a new script and add public fields in it. Will name it AbilityAdditionalSettings for example.
using UnityEngine;
// Just an example component for ability, which need additional info from unit object.
// Add this component to your unit prefab and now you can get it from ability code using unitOwner.GetComponent();
public class AbilityAdditionalSettings : MonoBehaviour
{
public Transform CustomTransform;
public GameObject CustomGameObject;
}
Add this component to your unit prefab and setup your fields as you need. Next, in ability you can get this component using this code:
using InsaneSystems.RTSStarterKit.Controls;
using UnityEngine;
namespace InsaneSystems.RTSStarterKit.Abilities
{
[CreateAssetMenu(fileName = "SomeAbility", menuName = Storage.AssetName + "/Abilities/Some Ability")]
public class SomeAbility : Ability
{
AbilityAdditionalSettings additionalSettings;
protected override void InitAction()
{
additionalSettings = unitOwner.GetComponent<AbilityAdditionalSettings>();
}
}
}
It will once load component from unit owning this ability, and next you can get any of your data in another ability code like this:
using InsaneSystems.RTSStarterKit.Controls;
using UnityEngine;
namespace InsaneSystems.RTSStarterKit.Abilities
{
[CreateAssetMenu(fileName = "SomeAbility", menuName = Storage.AssetName + "/Abilities/Some Ability")]
public class SomeAbility : Ability
{
AbilityAdditionalSettings additionalSettings;
protected override void InitAction()
{
additionalSettings = unitOwner.GetComponent<AbilityAdditionalSettings>();
}
protected override void StartUseAction()
{
additionalSettings.CustomTransform.position += Vector3.forward;
additionalSettings.CustomGameObject.SetActive(false);
}
}
}
So, you can make any abilities you want this way.