-
-
Notifications
You must be signed in to change notification settings - Fork 304
How to add custom button actions (1.7)
Written by @IvythePoS
Note: If you don't know anything about coding, this tutorial might not be for you.
Action | Keyboard | Gamepad |
---|---|---|
Player | ||
Move | WASD / Arrow Keys | Left Stick / D-Pad |
Jump | Space / Z | A / B Buttons |
Sprint | Shift / X | X / Y Buttons |
Powerup Action | E / C | Right Shoulder / Right Trigger |
Reserve Item | Q / V | Left Shoulder / Left Trigger |
UI | ||
Show Scoreboard | Tab | (Select/View) |
Pause | Escape | (Start/Menu) |
Debug | ` | --- |
- Open
Assets/Controls.inputactions
- If you want your new action to be a part of the game itself, click on "Player" at the left below "Action Maps." Otherwise, click on "UI."
- Add a new action by clicking on the
+
symbol at the far right of "Actions"
- Name your new action accordingly. This is important as this is how it will be called on the control settings.
- Add a total of four bindings by clicking on the + symbol at the far right of your action and clicking "Add Binding."
- In the first two bindings, check "Keyboard" below "Use in control scheme" at the right. For the last two, check "Gamepad."
-
Set the first two to the appropriate keyboard keys via the drowpdown "Path". Recommended keys are "R [Keyboard]" and "F [Keyboard]," but this can be entirely up to you.
-
Set the last two to the appropriate gamepad buttons. we are going to use "Left Trigger [Gamepad]," but this can be entirely up to you. Note: If the second button binding is empty, please don't delete the secondary binding. Just leave it empty.
- If your button binding is already taken on the list above (e.g. "Left Trigger [Gamepad]"), remove the button binding already used in the list. You can also replace it with another key.
- Make sure you had Auto-Save all along. If not, click on "Save Asset."
The button bindings are mostly automatically programmed in the game's settings and in Controls.cs
. We do not need to modify anything else to make the game recognize these bindings. We still need to figure out what they'll do. We'll assume we're going to have Mario/Luigi learn a new move.
-
Find
public void Awake()
inside the script. Inside, you'll find something that looks like this:if (photonView.IsMine) { InputSystem.controls.Player.Movement.performed += OnMovement; InputSystem.controls.Player.Movement.canceled += OnMovement; InputSystem.controls.Player.Jump.performed += OnJump; InputSystem.controls.Player.Sprint.started += OnSprint; InputSystem.controls.Player.Sprint.canceled += OnSprint; InputSystem.controls.Player.PowerupAction.performed += OnPowerupAction; InputSystem.controls.Player.ReserveItem.performed += OnReserveItem; }
-
Select a pre-existing action, then either copy and paste it on a new line inside the condition or select the line and duplicate it by pressing CTRL+D:
InputSystem.controls.Player.PowerupAction.performed += OnPowerupAction;
-
Now, rename the action on the duplicated line to your new action. There will be an error saying that it can't find a method with the new name, ignore that for now:
InputSystem.controls.Player.PowerupAction2.performed += OnPowerupAction2;
-
Repeat from Step 3, except in
public void OnDestroy()
:InputSystem.controls.Player.PowerupAction2.performed -= OnPowerupAction2;
-
Go to the "CONTROLLER FUNCTIONS" region and add a new public method with the same name that you created in steps 4 and 5 (referenced in both
Awake
andOnDestroy
). You can modify this to your liking (that is, if you're comfortable with coding), though it's recommended to have the action early return with the conditions:if (!photonView.IsMine || dead || GameManager.Instance.paused) return;
. In this case, since we're doing an alternate version ofOnPowerupAction()
, you may duplicate it while renaming all references to the previous action to your current action:// Renamed from "OnPowerupAction" to "OnPowerupAction2" public void OnPowerupAction2(InputAction.CallbackContext context) { if (!photonView.IsMine || dead || GameManager.Instance.paused) return; powerupButtonHeld = context.ReadValue<float>() >= 0.5f; if (!powerupButtonHeld) return; ActivatePowerupAction2(); }
-
The text on the second-to-last line above is an action that calls another method. You may add another method and specify what it will do, but since we're doing an alternate version of
ActivatePowerupAction()
, we'll duplicate the method, again renaming it to your current action:private void ActivatePowerupAction() { if (knockback || pipeEntering || GameManager.Instance.gameover || dead || Frozen || holding) return; switch (state) { case Enums.PowerupState.(Powerup): { // Add your code here. } } }
-
Congrats! Feel free to test the button bindings in-game and check if you can rebind them. Isn't this satisfying?
Thanks for reading!