Skip to main content

Lock

A class in Root.Township, Alta.Inventory.

A lock that opens when its matching key prefab is pushed in and turned far enough. Used on chests (Chest) and subclassed by ConsumableLock; the server decides when it unlocks and syncs the state.

Constructors​

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

Fields​

MemberWhat it is
protected KeyItem currentKeyThe key currently inserted, or null.
protected bool hasKeyWhether a key is held in the lock by its joint.
protected ConfigurableJoint keyJointThe joint holding the inserted key; breaks when the player pulls their hand too far away.
protected NetworkRigidbody rigidBodyThe lock's NetworkRigidbody, cached on Awake.
20 private and internal fields
MemberWhat it is
private Collider[] collidersToIgnorePrivate: the lock's colliders, also exposed as Colliders.
private Coroutine coolDownCoroutinePrivate: the running grab cooldown.
private uint correctKeyHashPrivate: prefab hash a key must have to fit this lock.
private NetworkPrefab correctKeyPrefabThe key prefab that fits this lock.
private float delayBetweenUnlockingAndOpeningSeconds between the key turning and the lock opening. Defaults to 1.
private float distanceToBreakJointHow far the hand can be from the held key before the key is pulled out. Defaults to 0.2.
private float dotToUnlockThe key must turn past this angle (stored as a dot product) to unlock.
private bool isDetachedLockBacking field for IsDetachedLock. A detached lock switches layer so players can't touch it while locked.
private bool isLockedBacking field for IsLocked.
private LockEvent isLockedEventBacking field for IsLockedEvent.
private bool isOutOfCooldownPrivate: false for a second after the key is grabbed, which blocks unlocking.
private KeyItemEvent keyTurnEventBacking field for KeyTurnEvent.
private Transform lockPositionWhere the key tip sits and which way it faces when inserted.
private int nonInteractingLayerPrivate: the NotInteractingWithPlayerBody layer used while a detached lock is locked.
private int originalLayerPrivate: the lock's own layer, restored when unlocked.
private KeyItemEvent receivedNewKeyEventBacking field for ReceivedNewKeyEvent.
private float sqrBreakDistancePrivate: distanceToBreakJoint squared.
private MethodSync syncBreakKeyGripPrivate: message for SyncBreakKeyGrip.
private MethodSync syncBreakKeyJointPrivate: message for SyncBreakKeyJoint.
private MethodSync syncLockedPrivate: message for SyncLocked, also sent to players as they connect.

Properties​

MemberWhat it is
Collider[] Colliders { get; }All colliders under the lock, ignored against the key while the key is inserted.
bool IsDetachedLock { get; }Whether this lock is a separate object. Detached locks switch to a layer that ignores the player body while locked.
bool IsLocked { get; }Whether the lock is currently locked.
LockEvent IsLockedEvent { get; }Raised whenever the lock becomes locked or unlocked.
KeyItemEvent KeyTurnEvent { get; }Raised when the key has been turned far enough and is released from the player's hand.
KeyItemEvent ReceivedNewKeyEvent { get; }Raised when the matching key enters the lock and is jointed in place.
NetworkRigidbody Rigidbody { get; }The lock's NetworkRigidbody.

Methods​

MemberWhat it is
override void Initialize()Sets up the synced lock state, key joint break and key grip break messages.
void JingleJangle(Vector3 jingleJangleTorque)Adds a torque to the lock's rigidbody to rattle it, unless a key is inserted. Called by Chest when shaken.
override void OnConnected(Player player)Server: sends the current locked state to a newly connected player.
void SetLock(bool isLocked)Sets the locked state, raises the event and, on the server, syncs it to clients. Does nothing if unchanged.
protected virtual void Locked()Called when the lock becomes locked; sets the flag and raises IsLockedEvent. Override to add behaviour.
protected virtual void Unlocked()Called when the lock becomes unlocked; clears the flag and raises IsLockedEvent. Override to add behaviour.
13 private and internal methods
MemberWhat it is
private void Awake()Caches the colliders, rigidbody, layers and squared break distance, and the hash of the correct key prefab.
private void BreakKeyGrip()Private: forces every hand off the inserted key, freezes it and raises KeyTurnEvent. The server syncs this to clients.
private IEnumerator CollidersIgnoreDelay(IEnumerable<Collider> colliders) (iterator)Private: turns collisions between the lock and a removed key back on after half a second.
private void LateUpdate()Breaks the key joint when the local hand pulls too far, and on the server starts unlocking once the key is turned far enough.
private void OnJointBreak(float breakForce)Private: releases the key when its joint breaks, giving physics back to both the lock and key.
private void OnTriggerEnter(Collider other)Private: when a key of the correct prefab enters the trigger, freezes both and seats the key with SetupKey.
private void RecentGrab(Interactable key, Interactor interactor)Private, server: starts a one second cooldown whenever the inserted key is grabbed.
private IEnumerator RunGrabCooldown() (iterator)Private: waits one second, then allows unlocking again.
private void SetupKey(KeyItem newKey)Private: snaps a key into the lock and joins it with a joint that can only twist, up to 90 degrees.
private void SyncBreakKeyGrip(IPlayer player, Stream stream)Private: client side of BreakKeyGrip.
private void SyncBreakKeyJoint(IPlayer player, Stream stream)Private: breaks the key joint when a client reports pulling out the key; the server relays it to other players.
private void SyncLocked(IPlayer player, Stream stream)Private: syncs the locked state, applied on clients with SetLock.
private IEnumerator UnlockDelay() (iterator)Private: breaks the key grip, waits delayBetweenUnlockingAndOpening, then unlocks.

Used in​

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