Buttons and Joystick UI input system with a FPS Example
Developed in Unity 2020.3.7f1
Didn't test this with older Unity versions, but it should work fine!
Uses the FREE asset Joystick Pack from the asset store for the joysticks! Asset made by Fenerax Studios!
- Add your desired actions in InputEnums.cs
namespace UI_Inputs.Enums
{
public enum ButtonAction
{
Jump,
Action1,
Action2,
Action3,
Action4
}
public enum JoyStickAction
{
Movement,
CameraLook
}
}
- Assign "UI_InputButton.cs" to a UI element with an image (clickable area)!
- Choose the desired action for the button created from the InputEnums! Example: Jump, Run, Shoot...
Obs: Compatible with: "Click Trigger", "Touch Trigger", "Hold Trigger" and "After Click Trigger"
- Assign "UI_InputJoystick.cs" to a Joystick from the asset pack (can be any type of joystick [Fixed, Floating etc...]).
- Choose the desired Joystick Action for the Joystick created from the InputEnums! Example: joystickCamera, joystickPlayerMovement...
- These are the default functions to call in your script (bool, float and Vector2 ready).
- If you want to add other type of functions add them to UI_InputSystem.
public static Vector2 GetAxisValue(JoyStickAction joystickToChek) => JoyStickProcessor(joystickToChek);
public static float GetAxisHorizontal(JoyStickAction joystickToChek) => JoyStickProcessor(joystickToChek).x;
public static float GetAxisVertical(JoyStickAction joystickToChek) => JoyStickProcessor(joystickToChek).y;
public static bool GetButton(ButtonAction buttonToCheck) => ButtonPressProcessor(buttonToCheck);
- Call "UI_InputSystem" on your desired controllers, add the desired command wanted and done!
- You can also subscribe to OnClick and OnTouch events for your actions.
- Remeber to add UI_Inputs.Enums namespace in your script to use ButtonAction and JoyStickAction Enums.
using UI_Inputs.Enums;
//Jump Example
private void OnEnable()
{
UIInputSystem.ME.AddOnTouchEvent(ButtonAction.Jump, ProcessJumping);
}
private void OnDisable()
{
UIInputSystem.ME.RemoveOnTouchEvent(ButtonAction.Jump, ProcessJumping);
}
void ProcessJumping()
{
if (!canJump) return;
if (isGrounded)
gravityVelocity.y = JumpForce();
}
//Movement Example
Vector3 PlayerMovementDirection()
{
Vector3 baseDirection = playerTransform.right * UI_InputSystem.ME.GetAxisHorizontal(JoyStickAction.Movement) +
playerTransform.forward * UI_InputSystem.ME.GetAxisVertical(JoyStickAction.Movement);
baseDirection *= playerHorizontalSpeed * Time.deltaTime;
return baseDirection;
}
Obs: This code is located in "PlayerMovement.cs"
Obs: Uses my Simple Character Controller adapted to use with this UI input system.