Hello,
I need to do a custom input manager for my game since in the default one I cannot set any value at runtime.
I know about the new Input system but I need something stable and the only thing that I really miss in the current Input Manager is the fact that I cannot set values.
My current idea is to make a simple Dictionary with a string as a key and a KeyCode as value… When I start the game of course I have a system to set all values in my Dictionary.
When I want to get my keycode for “Jump” key (example) I can check it with:
if(Input.GetKeyDown(CustomInput.GetInput(“Jump”))) where CustomInput.GetInput is a static method that return a KeyCode (example KeyCode.Space)… When I need to set a new KeyCode I just need to call CustomInput.SetInput(“Jump”, KeyCode.J) and save it, the same code to check will now answer to my new key “J”.
That’s easy…
The problem is that I also want to be able to remap Axis… The main problem is not that there is more keycode (positive and negative) for one string (that’s easy to fix)…
My problem is that I don’t know how to read the value of a KeyCode. I need to be able to read a value from -1 to 1 based on the joystick player input.
I know that there are many already made custom input manager (many of them are also free), but, if possible, I want to avoid to use something big for a small problem, something that I will not be able to update and that I don’t really understand (every custom input manager do a lot more than what I really need right now). I will use already made things when I need, but this time I think that I can avoid them if I can understand how Unity return a float from -1 to 1 when I use GetAxis.
Thank you to everyone that will try to answer to my question.
That’s good but only if I use a keyboard. If I use a joystick there are also 0.1,0.2,0.3…1 and -0.1,-0.2,-0.3…-1 values. I need to get every values, not just 1 and -1.
Thank you anyway
I’m fairly sure that unity creates it’s on smoothing when you’re talking about keys. Move them towards -1 or 1 and let them fade back to 0 when not pressed, etc…
I think you’d have to recreate that, if you wanted it for keys…
For a joystick, keep an input manager mapping to it as an axis. Whenever the user remaps a control to use the joystick, queries report back based on your input manager one.
Thank you but there is one thing I don’t understand. If I use a joystick I’m able, with GetAxis, to get a float value that it’s always based on how much the user push the joystick (for example I can get a value of 0.5 not just in the beginning).
I know how to create a smoothing effect with a Coroutine based on time (so that my value go from 0 to 1 not
instantly) but I don’t know how to base that value on the current joystick real value.
I made something like that for a touchscreen virtual joystick and for that I used my screen position… I don’t know what unity use for real joystick.
The idea to only use the default Input Manager when the user use a joystick is ok. But, if possible, I want the user to be able to remap the gamepad joystick as well.
I knew what you meant. What I was suggesting is that you can have your custom input manager for keys and a joystick…
For the joystick, you would also have an input manager axis (well, 2 of them, I suppose*).
So, if the user is say asking: float h = CustomInput.GetAxis(“Run”)
Your code would maybe say “if it’s a joystick, return Input.GetAxis(“InternalJoyHorizontal”);”
Main problem is that if I use:
Input.GetAxis(“InternalJoyHorizontal”);
I need to configure “InternalJoyHorizontal” in the default Input Manager and I cannot do that at runtime (there is no SetAxis) so the user will only be able to do that before the game start.
I want the user to be able to rebind any joystick at runtime.
there are also problems because different gamepad use different configurations… mobile/mac/linux it’s different from windows. That’s why I cannot configure joystick on the default Input manager, there are many gamepad that I don’t even own.
Right now I know how to capture a generic button KeyCode… (so if the user use a joystick I can get the boolean value of the user using it), but I don’t know how to read the current value of that KeyCode (that for a real button should be 0 not pressed, and 1 pressed, while for a joystick should return also mid values).
Maybe it’s not possible… but since there are many custom input manager that are not made by Unity I think there should be a way…
Hmm… Well, they may be made more natively, some of those
I wasn’t suggesting that the user be able to remap your internal joystick axis. Only that you could “point” their joystick mappings to your internal one simply to get a value in the range: -1 … 1
So, if they had “Run” to the joystick, you return the internal one. Then they remap it from Run to ‘strafe’ (sorry, not being too creative), again you simply fetch the input manager internal one.
To the user it appears to be working, yes?
I think the main problem with your idea is that Input.GetAxis(“name”) return a value != 0 only if the key used by the user is mapped in the Input Manager.
Let’s assume W is a joystick.
Input.GetAxis(“Vertical”) return a value between -1 and 1 only if the Input Manager contain a Vertical configured with W.
Even if I return Input.GetAxis(“Vertical”) with a different KeyCode it would return 0.
Maybe you are right, maybe they are using something more native… but maybe it’s just that I don’t know how to read the value of a KeyCode.
If it’s not possible I will just try to use the new input system… It will just require lot’s of update since it’s still not stable.
PS. I already have to update NavMeshComponents every release (and that’s stable… it’s just not included by default into unity).
Well, if I understood your comment correctly, you said it wouldn’t work if you didn’t map the internal input manager for horizontal and vertical?
If that’s what you meant, I completely agree…those have to be mapped, by you (not the player).
Their mappings could use the joystick (horz/vert) and have names, in your custom input.
If they choose the joystick and assign it to a custom input name, when you look it up, you have to re-direct to look it up as the ones from Unity’s input manager.
I hope that is more clear. If you meant you already understood that & it still sounds wrong to you, that’s okay, too, but I’m out of ideas on how to explain it
I, too, am looking forward to seeing their new input system. lol
When you use the default Unity’s Input Manager you need to give a:
name
positive button
negative button
“Their mappings could use the joystick (horz/vert) and have names, in your custom input.
If they choose the joystick and assign it to a custom input name, when you look it up, you have to re-direct to look it up as the ones from Unity’s input manager.”
this part would work only if “Input.GetAxis(name)” return a value. And that’s possible only if the user is using positive button or negative button… if the user is using a different button (because he remapped his input in my custom input) and I still redirect him to “Input.GetAxis(name)” I will get a zero, because Unity’s Input Manager will still only react to the positive/negative original button… if I try to get a different “name”, it should be setted by me in Unity’s Input Manager so that’s not an option.
If you mean something else can you give me a pseudo code so that I can understand it better?
Thanks
is still a valid piece of code but Input.GetAxis(“InternalJoyHorz”); return 0 because the user is using leftjoystick2, rightjoystick2… and Input.GetAxis(“InternalJoyHorz”); still answer to:
positive leftjoystick1
negative rightjoystick1
Of course if I set up
InternalJoyVert
positive leftjoystick2
negative rightjoystick2
it should work again… than I should also set up
positive leftjoystick3
negative rightjoystick3
ecc…
Maybe there is still something that I don’t get about your code…
Okay… I didn’t talk at all about multiple joysticks.
Let’s assume for now we’re just discussing 1 (to avoid potentially more confusion lol). For more than 1 joystick, you’d have to make an entry for each joystick (internal ones, I suppose*).
My only point was to show that you could map what used to be ‘Dodge’ in the example, to say “Jog”… now using the joystick for that, instead.
It’s just special for joysticks because, as you said, I think the value comes from how far the joystick is moved. (I’ve never used a joystick! )
You can’t just make up your own smoothing for that…
Ok I have tried with an actual gamepad… I have a better idea of what is and what is not possible:
If I use only 1 gamepad, no matter how many joysticks, I can use the option “Get Motion from all Joystick” and it will get a value even if I don’t set a positive/negative button. So I can remap every button in a gamepad like I remap keyboard button, I can return a float value -1 or 1 if I use a button to get axis (with a coroutine to smooth a little)… And I can return Input.GetAxis(“VerticalGamepad”) (example) and always get a value, I just need to set an Input in the Unity input manager for every axis that I want to read (example 4th and 5th is the secondary joystick on xbox 360 gamepad).
So it’s not the button value but the axis that count for joysticks.
If i want to use multiple Gamepad I just need to import an Input manager like the one made for OInput. It’s much easier that add everything by hands and I don’t even need to implement the OInput code, I can just import the Input Manager configuration.