- Home /
Switching between Inputs
I'm trying to wrap my head around the unity input system, and I've figured out how to directly use inputs, but Im wanting to indirectly use them via the action map. but in doing so I've come to a big hole in the road, how do I switch between left stick input and wasd? because using left stick i use readvalue and I'm assuming for wasd ill be using getaxis Raw/ Get axis if that works... I'm honestly overwhelmed with all this and I just need an explanation and a brief demo on this so I can continue working on my "game"... it's taken me 4 days to get the perfect moving scripts imo but i didnt use the input system now i want to convert so i can use a game pad.
Answer by Ermiq · Mar 13, 2021 at 10:42 AM
That's where inheritance comes to help us.
You can create a base class that won't use any input methods and will just work with already evaluated and prepared values and will send the values to the movement system, and a couple of classes derived from the base class that will get direct inputs.
I usually create a base class PlayerInputBase
that has, for example, moveVector
, rotationVector
, isRunning true/false
, etc.
And for WASD/mouse inputs I create a new class PlayerInputPC
that is derived from PlayerInputBase
that gets player inputs directly from Input.GetAxis()
and such and applies the values to the appropriate fields of the base class.
Same for touch inputs, for example: PlayerInputsTouch
that gets input values from the touch joystick and UI buttons and sends the same type of values to the base class methods.
And in the movement script you can use something like transform.position += PlayerInputBase.moveVector
- you don't have to use the exact name of PlayerInputPC
or PlayerInputTouch
, the movement system only knows the input reader class as PlayerInputBase
and it has all the fields ( moveVector
, isRunning
, etc.).
Simple code structure example:
public class PlayerInputBase : MonoBehaviour
{
// This class shouldn't be attached to gameObjects.
// For actual input reading use either PlayerInputPC
// or PlayerInputGamepad scripts.
// The fields that will be evaluated by either PC or
// Gamepad class and the movement script will read these values.
public float moveX;
public float moveY;
protected virtual void Update() { }
}
// For PC inputs:
public class PlayerInputPC : PlayerInputBase
{
protected override void Update()
{
base.Update();
// ^^ ^^ Never forget to call the base class method as well.
// In this example the PlayerInputBase.Update() does nothing,
// but if it were doing something we would
// certainly need the Update() in base class be executed
// Get inputs as PC inputs and store them as moveX and moveY
// This class inherits these fields from the base class
moveX = Input.GetAxis("Mouse X");
moveY = Input.GetAxis("Mouse Y");
}
}
// For gamepad inputs:
public class PlayerInputGamepad : PlayerInputBase
{
protected override void Update()
{
base.Update();
// I don't actually know how you read gamepad values
// so here I use pseudocode
moveX = Gamepad.current("X");
moveY = Gamepad.current("Y");
}
}
public class MovementScript : MonoBehaviour
{
// Here we don't care whether the actual input reading class is PlayerInputPC or PlayerInputGamepad.
// In both cases it's also a PlayerInputBase as well, so we use it as just PlayerInputBase.
PlayerInputBase playerInput;
void Update()
{
Vector2 move = new Vector2(
playerInput.moveX,
playerInput.moveY);
transform.position += move * Time.deltaTime;
}
}
Note:
This approach is designed in such way that you also should decide which input reading class you will use. Only one of them should be active in the scene (attached to some GameObject). Otherwise, one of them won't work, because the movement script will only read moveX, moveY
from only one of the input classes.
For example, you can attach PlayerInputPC
to one gameObject and PlayerInputGamepad
to some other object, and depending on whether the player uses gamepad or not activate/deactivate the gameobject with the class that is needed/not needed right now.
Thank you for this, I'm going over this answer now Nd I'm going to start working on it now I appreciate your help!
Your answer
Follow this Question
Related Questions
Unity's 2D Gamekit Input problem 1 Answer
Unity input system only triggers once 2 Answers
Gamepad and keyboard act differently in multiplayer game using Input System 0 Answers
Multiple Players on one Computer/Console 5 Answers
How to find out if a button has been released (new input system/gamepad) 0 Answers