I’m making a virtual controller, and I was wondering if I could manually change an Input.Axis’s value. For example, when a game object is at a certain location, I want to be able to change the axis(“horizontal”) value to 1 or -1, without the help of hardware.
From what I’ve seen, I can only call GetAxis(name), but I’d like something like SetAxis(name, x). Is there any way to do this?
To answer my own question, it appears that you can not set custom values in the Input class. What I ended up doing was writing my own CustomInput class that allows for setting and getting input values.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class CustomInput {
static Dictionary<string,float> inputs = new Dictionary<string, float>();
static public float GetAxis(string _axis){
if(!inputs.ContainsKey(_axis)){
inputs.Add(_axis, 0);
}
return inputs[_axis];
}
static public void SetAxis(string _axis, float _value){
if(!inputs.ContainsKey(_axis)){
inputs.Add(_axis, 0);
}
inputs[_axis] = _value;
}
}
All values are automatically added if they don’t already exist. Use CustomInput.GetAxis(name) to get, and CustomInput.SetAxis(name, value) to set.
The only problem is, I’m not quite sure how optimized this will be for mobile, but feel free to use it anyways.
I’ve found another way - you can set input.GetAxis to a float and then change it’s value.
private float horizontalInput;
void Update() {
horizontalInput = Input.GetAxis(“Horizontal”);
if (horizontalInput > 0) {// Move right}
// Same as
if (Input.GetAxis("Horizontal") > 0) { // Move right}}
That means you can change the horizontalInput if you want to but otherwise it will be equal to Input.GetAxis(“Horizontal”) - for example you can set it to a certain value if it meets a condition:
void Update() {
if (!shouldMove) {
horizontalInput = 0;
}
else
{
horizontalInput = Input.GetAxis(“Horizontal”);
}
}