- Home /
Is there a way to manually change the Input.Axis values?
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?
Not as such, but you could write your own input manager which wraps around Unity's input classes and provides the functionality you're looking for. For example, reading from an input axis normally except for when you set some override value.
One thing you'll want to clarify, if you go that route: how long does this override last for? Some amount of time? Until some in-game event?
Depending on the exact features you're looking for, that can be fairly quick or extremely involved. I've found that most complex projects wind up doing at least some input wrapping, sooner or later.
I figured as much. That's unfortunate, I believe the Input class could be much more powerful if it allowed for manual value entries.
That said, what I've done is just create a CustomInput class that allows for setting and getting an input value. It's not as in-depth as the current Input class, but it works just as I'd like it to. :)
Answer by tonycoculuzzi · Jun 07, 2012 at 08:23 AM
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.
Another alternative - use CrossPlatformInput$$anonymous$$anager from Standart assets. See joystick script there as an example. To get values use: CrossPlatformInput$$anonymous$$anager.GetAxis(name) CrossPlatformInput$$anonymous$$anager.GetAxisRaw(name)
I have a certain use case wherein I could use your CustomInput
class to get
and set
the axis value. The only caveat is that I'm using a library (Ros#) which uses Input.GetAxis(axisName)
in some of their scripts. How would I expose the CustomInput
to the Unity's own Input
so that I could use this functionality without having to change much of the code in the library?
PS. I don't have much experience in Unity and I'm just beginning to learn concepts of Unity!
Answer by RandomAndrei · Jul 12, 2019 at 04:00 AM
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"); }
}
That's actually a really good solution that's so obvious that it's unintuitive.
Your answer
Follow this Question
Related Questions
Steering Wheel / joystick GetAxis trouble 0 Answers
How to make new Axis? 2 Answers
New Input System How to get Left Stick Axis 1 Answer
Reading 'not defined' Controller Axis Input? 0 Answers
Problem with input code for a camera follow/rotate script 0 Answers