-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
This input system is designed for ease of use and flexibility. All that is necessary for a GameObject to start receiving input is to set up keybinds, add the correct type of InputComponent, and set up another component to query the InputComponent.
TODO: Explain
using UnityEngine;
using InputSystem.KeyboardMouse;
using InputSystem.Components;
using InputSystem.XboxGamepad;
public static class Binds
{
public static InputSystem.VectorKeybind MoveBind = new InputSystem.VectorKeybind("move", "This Is A Pretty Name", "Here you can describe what the key will do");
public static InputSystem.VectorKeybind AimBind = new InputSystem.VectorKeybind("aim"); // above params aren't necessary
public static InputSystem.ButtonKeybind JumpButton = new InputSystem.ButtonKeybind("jump");
public static InputSystem.ButtonKeybind SprintButton; // in fact, you don't have to assign a string ID if you don't want to
[InputSystem.Attributes.BindingGenerationMethod]
static void GenerateBindings()
{
// Set up defaults if we haven't loaded in
if (!MoveBind.Bind.AnyBindings)
{
MoveBind.Bind
// Assign the keyboard keys WASD to map to a vector
.SetKeyboardVector(KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D)
// And assign the left stick to map to a vector
.SetXboxStick(XboxStick.LeftStick);
}
if (!AimBind.Bind.AnyBindings)
{
AimBind.Bind
// Use mouse look
.SetUseMouse()
// Use the right stick
.SetXboxStick(XboxStick.RightStick);
}
if (!JumpButton.Bind.AnyBindings)
{
JumpButton.Bind
.SetKeyboardMouseButton(KeyCode.Space)
.SetXboxButton(XboxButton.A);
}
if (!SprintButton.Bind.AnyBindings)
{
SprintButton.Bind
.SetKeyboardMouseButton(KeyCode.LeftShift)
.SetXboxButton(XboxButton.LeftStick);
}
}
}TODO: Explain.
using UnityEngine;
using InputSystem.KeyboardMouse;
using InputSystem.Components;
using InputSystem.XboxGamepad;
public class MoveSprite : MonoBehaviour
{
// Lazily loading the input controller
private InputComponent _inp;
public InputComponent Input
{
get
{
if (!_inp)
{
_inp = GetComponent<InputComponent>();
}
return _inp;
}
}
void Update()
{
// If we have a valid input controller...
if (Input)
{
float mult = 1;
// If we are holding down the sprint button (shift or left stick by default)
if (Input.GetButtonHeld(Binds.SprintButton))
{
// up our speed
mult *= 2;
}
// Move with wasd + mouse OR left/right stick
transform.position += (Vector3)((Input.GetVector(Binds.MoveBind) + Input.GetVector(Binds.AimBind)) * Time.deltaTime) * mult;
}
// If we press the jump key (space or A)...
if ((Input && Input.GetButtonPressed(Binds.JumpButton)))
{
// reset our position (because that's what a jump does, ofc)
transform.position = Vector3.zero;
}
}
}Absolute inputs return screenspace position. This is currently only implemented for mouse position. The following snippet sets a keybind to use absolute mouse position for KBM or the right stick for Xbox:
Aim.Bind
.SetUseAbsoluteMousePosition() // Use absolute mouse position
.SetXboxStick(XboxStick.RightStick); // Otherwise, use the right stickThe following code checks if the binding is absolute and, if so, sets the desired AimVector to the normalized vector from the player to the screenspace position.
Note: This works as-is for a 2D game (which was the source of the code), but for a 3D game, you would need to do a raycast.
Otherwise, it normalizes the relative vector as a direction.
...
var aimVector = Input.GetVector(Keybinds.Aim);
if (Input.IsBindAbsolute(Keybinds.Aim))
{
var dir = (aimVector - (Vector2)Camera.Main.WorldToScreenPoint(transform.position));
AimVector = dir.normalized;
}
else
{
if (aimVector.sqrMagnitude > 0.1f)
{
AimVector = aimVector.normalized;
}
else
{
AimVector = Vector2.zero;
}
}
...