Keyboard Axis#
Input / Keyboard Axis
Can Read
Cannot Write
Description#
Returns a single axis of the WASD or the Arrow keys
Parameters#
| Name | Description |
|---|---|
| Keys | Which set of directional keys is read |
| Axis | Whether the horizontal or the vertical value is returned |
Code#
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Keyboard Axis")]
[Category("Input/Keyboard Axis")]
[Description("Returns a single axis of the WASD or the Arrow keys")]
[Image(typeof(IconWASD), ColorTheme.Type.Blue, typeof(BadgeDot))]
[Parameter("Keys", "Which set of directional keys is read")]
[Parameter("Axis", "Whether the horizontal or the vertical value is returned")]
[Keywords("Move", "Keys", "Arrows", "Horizontal", "Vertical")]
[Serializable]
public class TypeNumberInputKeyboardAxis : TypeNumber
{
[SerializeField] private KeyboardDirections m_Keys = KeyboardDirections.WASD;
[SerializeField] private InputAxis m_Axis = InputAxis.Horizontal;
public override double Get(Args args)
{
Keyboard keyboard = Keyboard.current;
if (keyboard == null) return 0d;
(Key negative, Key positive) = (this.m_Keys, this.m_Axis) switch
{
(KeyboardDirections.WASD, InputAxis.Horizontal) => (Key.A, Key.D),
(KeyboardDirections.WASD, InputAxis.Vertical) => (Key.S, Key.W),
(KeyboardDirections.Arrows, InputAxis.Horizontal) => (Key.LeftArrow, Key.RightArrow),
(KeyboardDirections.Arrows, InputAxis.Vertical) => (Key.DownArrow, Key.UpArrow),
_ => throw new ArgumentOutOfRangeException()
};
double value = 0d;
if (keyboard[positive].isPressed) value += 1d;
if (keyboard[negative].isPressed) value -= 1d;
return value;
}
public override string ToString()
{
return $"Keyboard {this.m_Keys} {this.m_Axis}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}