- Home /
Change input axis values from code
I have a bool in my settings menu that is called inverted controls. This is because the arrow keys and WASD are both used for different things and some people might want to swap their functions in the game.
How can I change an input axis buttons from code and if that's not possible what is another way I could do it?
here is a simplified version of the code:
I want this to be (left arrow) and (right arrow) if the inverted controls bool is true
if (Input.getaxis("Horizontal"))
{
//move the player
}
I want this to be (a) if the inverted controls bool is true
if (Input.GetKey(KeyCode.LeftArrow))
{
//move the camera left
}
I want this to be (d) if the inverted controls bool is true
if (Input.GetKey(KeyCode.RightArrow))
{
//move the camera right
}
Answer by Eno-Khaon · Nov 17, 2021 at 09:56 AM
I'm a little confused by this; the "simplified" version makes this significantly easier:
float invertMultiplier = invertControls ? -1.0f : 1.0f;
float playerInputH = Input.GetAxis("Horizontal") * invertMultiplier;
Answer by BetaFruit · Nov 17, 2021 at 08:41 PM
I'm really stupid and I'm not quite sure how to add that in or what it would do. But basically, I'm trying to change the horizontal axis negative button from a to left arrow and the positive button from d to right arrow and two if statements checking for a press of the left or right arrow to a press of a or d.
Maybe there is a way to use alt buttons (alt negative button and alt positive button) to do this?
Well, let's break a few aspects of this down, then. First, the difference(s) between movement schemes:
// Using a different KeyCode per direction moved
Vector2 playerInput = Vector2.zero;
if(Input.GetKey(moveLeftKeyCode)) // e.g. KeyCode.LeftArrow
{
playerInput.x = -1f;
// play animation for moving left
}
else if(Input.GetKey(moveRightKeyCode)) // e.g. KeyCode.RightArrow
{
playerInput.x = 1f;
// play animation for moving right
}
// move player using playerInput, now that it's been obtained
// --------------------------
// Using an axis for movement, then adapting to it
Vector2 playerInput = new Vector2(Input.GetAxis("Horizontal"), 0f);
// apply movement using playerInput,
// since X-axis ranges from -1 to 1
if(!Mathf.Approximately(playerInput.x, 0f))
{
// play animation based on direction character is moving
}
Essentially, these are just two different ways of doing the same thing: Gathering Input/movement using discrete key/button presses (Input.GetKey()) or building input and applying it regardless of its value (Input.GetAxis()).
In either case, I'm deliberately separating "playerInput" from its usage to demonstrate how it can still be utilized in either implementation. This also makes it easier to factor in control disruptions:
// Slow player movement
float slowPlayer = 0.5f;
playerInput *= slowPlayer;
// Hasten player movement
float hastenPlayer = 1.5f;
playerInput *= hastenPlayer;
// Reverse player movement
float invertPlayer = -1.0f;
playerInput *= invertPlayer;
Your answer
Follow this Question
Related Questions
Reading 'not defined' Controller Axis Input? 0 Answers
Steelseries 3gc controller key mappings 0 Answers
Get list of Axes? 4 Answers
Button Axis with New Input System 0 Answers