Skip to content

Buttons and Joystick UI input system with a FPS Example

Notifications You must be signed in to change notification settings

playjoa/Unity_UI_InputSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

Unity_UI_InputSystem

Unity Asset Store Link!

Buttons and Joystick UI input system with a FPS Example

Developement info:

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!

SetUp

1. Create your actions

  • Add your desired actions in InputEnums.cs
namespace UI_Inputs.Enums
{
    public enum ButtonAction 
    {
        Jump,
        Action1,
        Action2, 
        Action3,
        Action4
    }

    public enum JoyStickAction
    {
        Movement,
        CameraLook
    }
}

2. Button setup

  • 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...

1
Obs: Compatible with: "Click Trigger", "Touch Trigger", "Hold Trigger" and "After Click Trigger"

3. Joystick setup

  • 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...

1

4. Create your inputs

  • 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);

5. Using inputs

  • 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"

FPS Example

1
Obs: Uses my Simple Character Controller adapted to use with this UI input system.

About

Buttons and Joystick UI input system with a FPS Example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages