Skip to main content

SelectionLever

A class in Root.Township, the global namespace.

A lever with a fixed number of positions between a min and max angle. Snaps to the nearest position on release and fires events. Not networked by itself.

Constructors​

MemberWhat it is
SelectionLever()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

21 private and internal fields
MemberWhat it is
private float angleOffsetDegrees added to the hand angle in GetLeverAngle.
private IUnityUpdateMethod clampLeverRegistered late update for ClampLever while the handle is held.
private float currentAngleBacking field for CurrentAngle.
private int debugIndexPosition used by the editor debug button.
private Transform gripPointGrip point of the hand holding the handle. Null when not held.
private Pickup handleBacking field for Handle.
private Transform handleParentTransform the handle goes back under when let go.
private bool isResetVelocityOnReleaseStops the handle's motion and makes it kinematic on release. True by default.
private bool isSettingToDebugIndexEditor button that runs SetDebugIndex.
private bool isUsingCustomAnglesIf set, selectionAngles is not recalculated in the editor.
private Transform leverTransform rotated to show the lever's angle.
private float maxAngleBacking field for MaxAngle. 160 by default.
private float minAngleBacking field for MinAngle. 15 by default.
private BoolUnityEvent notZeroEventUnity event raised with whether the new selection is not 0.
private int numberOfOptionsBacking field for NumberOfOptions. 2 by default.
private float perIndexDegrees between positions.
private bool preventInteractionWhenOverZeroIf set, the handle can't be grabbed while the selection is above 0 and is dropped when it moves there.
private float radiusDistance from the lever to the handle, measured in Start. Not read after that.
private bool runSelectionOnlyOnNotZeroIf set, selectionEvent only fires for selections above 0.
private float[] selectionAnglesBacking field for Selection: the angle of each position.
private IntEvent selectionEventUnity event raised with the new selection index.

Properties​

MemberWhat it is
float CurrentAngle { get; }The lever's angle while being held, clamped to the range.
int CurrentSelection { get; private set; }Index of the position the lever is set to.
Pickup Handle { get; }The Pickup the player grabs to move the lever.
float MaxAngle { get; }Angle of the last position.
float MinAngle { get; }Angle of the first position.
int NumberOfOptions { get; }How many positions the lever has.
float[] Selection { get; }The angle of each position.

Events​

MemberWhat it is
event Action<SelectionLever, int, int> SelectionChangedRaised with the lever, old index and new index when the selection changes.

Methods​

MemberWhat it is
void SetLeverToIndex(int index)Moves the lever to a position, clamped to the range, and fires the events if it changed.
11 private and internal methods
MemberWhat it is
private void CalculatePerIndex()Works out the angle between positions from the range and option count.
private void ClampLever()Late update while held: turns the lever to follow the hand, clamped to the range.
private void FilterWhenActive(Interactor interactor, Interactable interactable, ref bool result)Interaction filter that refuses grabs while the selection is above 0. Used with preventInteractionWhenOverZero.
private float GetLeverAngle()Angle of the gripping hand around the lever's X axis, plus angleOffset.
private void LimitLever(Interactable arg1, Interactor arg2)On grab, stores the hand's grip point and starts ClampLever each late update.
private void OnValidate()Editor: fills selectionAngles evenly across the range, unless custom angles are used.
private void ResetVelocity()Stops the handle's rigidbody and makes it kinematic.
private void SelectOnRelease(Interactable arg1, Interactor arg2)On release, snaps to the nearest position, resets the handle's velocity and puts it back on its parent.
private void SetDebugIndex()Editor button: moves the lever to debugIndex.
private void SetLeverAngle(float angle)Sets the lever's local X rotation.
private void Start()Measures the handle radius, works out the spacing and hooks the handle's grab, release and filter.

Used in​

Members of other types that take, return or hold a SelectionLever: