Remapping controls in-game

Particularly if your game is played with a keyboard or controller, you may want to provide your players with the option to remap the controls. It is possible to override Adventure Creator's input checks by using delegates - this also makes it possible to link AC with other input management assets.

In this tutorial, we'll override the InteractionA input, which is the input button used to interact with Hotspots, using a simple script. Be aware that 'delegates' are a slightly more advanced scripting topic, but the principles involved in here can be applied to create a completely customisable input system.

If you open up the Settings Manager, you can see a list of all inputs our game can use about halfway down:

Since we'll be overriding input, ensure that Assume inputs are defined? is left unchecked. This will mean Unity does not throw errors if any of these inputs are not defined.

Normally, we'd now need to create an input named InteractionA within our Input Manager. But instead: we're going to write a script that lets us change which keyboard key triggers this input.

Create a new C# script named InputOverride. Open it up, and add the AC namespace to it's header:

using UnityEngine;
using System.Collections;
using AC;

Now declare a public KeyCodevariable named interactionKey, that will represent the letter key mapped to InteractionA. Here, we've given it a default value of 'a':

public KeyCode interactionKey = KeyCode.A;

AC's PlayerInput script has a number of functions that serve the same purpose as Unity's Input.GetButtonDown (etc) functions - they are named similarly, but without the dot after 'Input', e.g. InputGetButtonDown. We'll write our own function that will override InputGetButtonDown. It takes a string parameter for the button's name, and returns a bool that determines whether or not the button was pressed:

private bool CustomGetButtonDown (string buttonName)
{
	return Input.GetButtonDown (buttonName);
}

Now let's modify the function so that, if buttonName is equal to InteractionA, we'll instead return the value of Input.GetKeyDown (interactionKey);.

private bool CustomGetButtonDown (string buttonName)
{
	if (buttonName == "InteractionA")
	{
		return Input.GetKeyDown (interactionKey);
	}
	return Input.GetButtonDown (buttonName);
}

Optionally, we can wrap Unity's "Input.GetButtonDown" call around a try/catch statement. This will prevent errors if an input is not defined:

private bool CustomGetButtonDown (string buttonName)
{
	if (buttonName == "InteractionA")
	{
		return Input.GetKeyDown (interactionKey);
	}
	try
	{
		return Input.GetButtonDown (buttonName);
	} catch {}
}

We now just need to assign our CustomGetButtonDown function as the delegate that override's PlayerInput's InputGetButtonDown function. We can do this by assigning it in our Start() function:

void Start ()
{
	KickStarter.playerInput.InputGetButtonDownDelegate = CustomGetButtonDown;
}

All available input delegates can be found listed in the scripting guide. The main page of the guide also describes how any of AC's "engine" scripts can be referenced in his way.

Our complete script is then as follows:

using UnityEngine;
using System.Collections;
using AC;

public class InputOverride : MonoBehaviour
{

	public KeyCode interactionKey = KeyCode.A;

	void Start ()
	{
		KickStarter.playerInput.InputGetButtonDownDelegate = CustomGetButtonDown;
	}

	private bool CustomGetButtonDown (string buttonName)
	{
		if (buttonName == "InteractionA")
		{
			return Input.GetKeyDown (interactionKey);
		}

		try
		{
			return Input.GetButtonDown (buttonName);
		} catch {}
	}

}

Attach this script to an empty GameObject in an AC scene, and run the game. You'll find that you can interact with Hotspots by pressing whatever key is listed as the interactionKey in the script's Inspector!

Note that this will only override input for the single scene. To have it affect multiple scenes, convert the GameObject into a prefab and place an instance of that prefab in each scene.

Update:
A further example, that involves linking AC to the Rewired asset, can be found here.