0
\$\begingroup\$

I need to have two players moving in a level for a school project and my player controller is already set up for using axis inputs (as opposed to discreet buttons) for directional input. I already know I can assign custom key inputs from the inspector if I just declare a public variable (initialization optional because there is a drop-down list in the inspector):

public KeyCode jumpInput = KeyCode.Space;

How can I assign custom directional axis inputs from the inspector? I already have alternative horizontal and vertical axes defined in my preferences, I just need to know how to assign them.

Been putting this project together for 36 CONTIGUOUS hours and I am ready to put it (and myself) to bed. Thank you in advance for your time.

EDIT: My confusion was coming from (beyond being awake too long) thinking that I needed a special data type (like KeyCode) for an Axis, and couldn't find it in the reference. But I didn't consider making a public string and plugging that into the input. This way, I just need to know what I named the new axes and use that for the public string.

\$\endgroup\$
  • \$\begingroup\$ Presumably you tried exposing your axis name as a public string variable, similar to your strategy for configuring keys? Where specifically did you run into trouble? \$\endgroup\$ – DMGregory Jul 20 at 14:20
  • \$\begingroup\$ I want to use the same controller script for both players so I wanted a drop-down list from the inspector so I could just have every "GetAxis()" invocation reference whatever axis the GameObject (player) is assigned (or similar). I don't want to write new methods or new scripts for each player. It's easy to change the label for a button or axis, but not refactoring the entire script \$\endgroup\$ – hatinacat2000 Jul 20 at 15:00
  • \$\begingroup\$ You would not need to write any new methods. This is only a public variable, the same technique you're using for your keys, except the value is a string instead of an enum. \$\endgroup\$ – DMGregory Jul 20 at 15:16
  • \$\begingroup\$ You are absolutely correct in your assumption, I just can't find documentation on how to do it for an axis like I was able to do it for a key. Keys have single keycodes; axes have the name I set in the project settings but also have negative and positive keycodes. Some people online said it can't be done but that was a few years ago. I'm looking for a way to assign the axes in the inspector that I made in my preferences (which basically amounts to removing the alternative axis keys up/down/, left/right and putting them in their own axes). \$\endgroup\$ – hatinacat2000 Jul 20 at 19:37
  • \$\begingroup\$ I'm not following. If you have an axis defined that's called "Player1Horizontal" then what's to stop you from making a variable like public string horizontalAxisName = "Player1Horizontal"; then using GetAxis(horizontalAxisName) to read it? For player 2, you'd just change the string in the inspector to read "Player2Horizontal" instead, a pure data change with no code changes required. What specific behaviour are you unable to achieve in this way? \$\endgroup\$ – DMGregory Jul 20 at 20:29
1
\$\begingroup\$

if you assigned a new Axes in the ProjectSettings>Input you can use it the same as you would the normal one. Assign the positive and negative buttons player 2 uses to move.

float newHorizontal = Input.GetAxis ("YourNewHorizontalInputName");
\$\endgroup\$
  • \$\begingroup\$ Wouldn't this require that my script reference what player it is attached to every time input is received? Is there a public method I can handle in the inspector? \$\endgroup\$ – hatinacat2000 Jul 20 at 15:03
  • \$\begingroup\$ You would set player2 to take input of your 2nd Axes for each one you set for the 2nd player. Was that what you want to achieve 2 separate players being controlled from same keyboard but using different axes to control? \$\endgroup\$ – Justin Markwell Jul 20 at 15:29
  • \$\begingroup\$ Yes, same keyboard. Are you saying to declare the above variable publicly and then I can just type the name of the axis in the inspector? That's what I would like to do \$\endgroup\$ – hatinacat2000 Jul 20 at 19:34
  • \$\begingroup\$ you can have it public, private, or local its up to you cause when you define a new Axes like "HorizontalMove2" for example. You can access the return value anywhere from any other MonoBehaviour script using Input.GetAxis("HorizontalMove2"); if that's the name you defined in the Axes \$\endgroup\$ – Justin Markwell Jul 20 at 23:44
  • \$\begingroup\$ I get what you were saying now \$\endgroup\$ – hatinacat2000 Jul 21 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.