Is it possible to fake inputs? So let’s say I wanted to do some automated stuff, could I just write a script to pretend such-and-such a button is held down?
You can’t simulate the hardware inputs directly, but you can write some special functions to accept both, the hardware inputs and yours, and use these functions instead of the original ones, like this:
var myVAxis: float; // this axis value will be returned if != 0
function InputGetAxisVertical(): float{
if (Mathf.Abs(myVAxis)>0.01){ // return myVAxis if >0.01 or <-0.01
return myVAxis;
}
else {
return Input.GetAxis("Vertical"); // return vertical axis if myVAxis = 0
}
}
// then use this function instead of Input.GetAxis("Vertical")
var vSpeed = InputGetAxisVertical();
In this case, if you assign any value >0.01 or <-0.01 to myVAxis, this value will be returned; if you set myVAxis to 0, the regular “Vertical” axis will be returned.
I also answered a question wtih a GUI joystick simulator, which uses this idea: click here to see this answer.
Usually when you perform some sort of action that depends on input, you’re just listening for the correct key press before triggering a specific behaviour; you would then call the appropriate function once this happens.
If you want to “fake” axis inputs, wouldn’t you just call the functions you want and cut out the middle man, or am I missing something here? For example:
if (your input)
GoThere();
versus
GoThere();
Make a wrapper for Input that has all the functions you need, and forward the calls to the real input, except when you are faking it.
like so:
class MyInput
{
static var isFake : boolean;
static function GetAxis (axisName : String) : float
{
if (isFake)
return Fake.GetAxis(axisName); // Could be recorded input etc...
else
return Input.GetAxis(axisName); // Otherwise, real input...
}
// ... Other Input.FunctionName wrappers here ...
// ... Other Input.FunctionName wrappers here ...
// ... Other Input.FunctionName wrappers here ...
// ... Other Input.FunctionName wrappers here ...
}
Then you can replace your usual calls to Input.GetAxis to MyInput.GetAxis, and control the behavior with MyInput.isFake. This makes it very easy to “slide your code” into your existing project since they will have the default behavior with a simple rename of Input to MyInput.
You’d naturally have to implement the fake class to suit your behavior.
Btw, I haven’t coded UnityScript for ages so I don’t know if the class follows correct syntax, but it looks good to me. If anyone spots an error, just edit the answer.