Skip to main content

Crank

A class in Root.Township, the global namespace.

A handle you grab and turn, giving a value from 0 to 1 that changes with each turn. Server side by default; set isRunningOnClients to work it out on each client instead.

Constructors​

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

Fields​

24 private and internal fields
MemberWhat it is
private Vector3 clampedOffsetPrivate: not used in this build.
private float currentAnglePrivate: the hand's angle around the axis at the last update.
private Interactor currentInteractorPrivate: the hand being tracked.
private float differencePrivate: angle change since the last update, in degrees.
private float directionPrivate: 1 for clockwise, -1 otherwise.
private Vector3 handOrientationAxisAxis the handle twists around to match the hand. Zero means use orientationAxis.
private bool isActivePrivate: not used in this build.
private bool isClockwiseWhether turning clockwise raises the value.
private bool isRunningOnClientsIf set, each client works out the value itself and the server does not sync it.
private Vector3 lastPositionPrivate: the hand's last position relative to rotationCenter.
private float lengthPerRevolutionHow much the value changes for one full turn. Defaults to 1.
private FloatEvent onRotatedEvent raised each update with the angle turned, in degrees.
private FloatEvent onValueChangedEvent raised with the new value.
private bool orientatesToHandWhether the handle twists to match the hand when held by one hand.
private Vector3 orientationAxisLocal axis the target turns around.
private Pickup pickupThe pickup players grab to turn the crank.
private bool rotatesAroundAxisWhether the target turns with the hand at all.
private Transform rotationCenterTransform the hand position is measured from.
private float rotationOffsetSerialized value not read in this build.
private MethodSync syncReleasePositionPrivate: message for SyncReleasePosition.
private MethodSync syncValuePrivate: message for SerializeValue.
private Transform targetThe transform that turns.
private IUnityUpdateMethod updateMethodPrivate: the registered fixed update while held.
private float valueBacking field for Value.

Properties​

MemberWhat it is
float Value { get; protected set; }The crank's value, from 0 to 1. Changing it raises ValueChanged. It is saved.

Events​

MemberWhat it is
event Action<float> DeltaProvidedRaised each update while turned, with the value change before clamping.
event Action<Crank, float> ValueChangedRaised with the crank and its new value whenever the value changes.

Methods​

MemberWhat it is
override void Initialize()Sets up the value and release rotation sync messages.
override void OnConnected(Player player)Sends the current value to a joining player.
12 private and internal methods
MemberWhat it is
private void Awake()Sets the turn direction, defaults the hand axis to the turn axis and hooks the pickup's grab and release.
private void OnValidate()Private, editor: updates the direction from isClockwise.
private void OnValueChanged()Private: raises ValueChanged and onValueChanged; the server also syncs the value unless clients run it.
private void SerializeValue(IPlayer player, Stream stream)Private: syncs the value. The server ignores values sent by clients.
private void SetDirection()Private: sets direction to 1 or -1 from isClockwise.
private void Start()Private: raises the value changed events once with the starting value.
private void StartTrack(Interactable interactable, Interactor interactor)Private: starts following the grabbing hand every fixed update.
private void StopTrack(Interactable interactable, Interactor interactor)Private: when the last hand lets go, stops tracking, makes the pickup kinematic and the local player syncs where it was left.
private void SyncReleasePosition(IPlayer player, Stream stream)Private: syncs the handle's rotation after release; the server relays it to other players.
private void TrackHands()Private: turns the target to follow the hand around the axis, and twists the handle to match the hand if orientatesToHand is set.
private void UpdateCrank()Private: fixed update while held. Tracks the hand, and updates the value on the server or when clients run it.
private void UpdateValues()Private: turns the angle change since the last update into a value change, raises the rotation events and clamps the value.

Used in​

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