Skip to main content

RaycastCrafter

A class in Root.Township, the global namespace.

Places a pickup onto a surface when it is let go: raycasts in a set direction and either sticks the pickup there or replaces it with another prefab. The server does the placing and syncs it; used by things like wall hooks.

Constructors​

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

Fields​

21 private and internal fields
MemberWhat it is
private Quaternion additionalRotationExtra rotation applied after facing the surface normal.
private AltaEvent craftedEvent fired when the pickup is placed. When loading as placed it only fires on the server.
private Coroutine currentCoroutineThe running preview or placing coroutine.
private Vector3 directionRaycast direction. Defaults to down.
private float distanceRaycast length. Defaults to 1.
private PooledObjectDefinition effectPooled effect shown at the surface while the pickup is held.
private const int entityPileSizeToConsiderGriefingNumber of overlapping entities (10) that counts as a griefing pile.
private static HashSet<uint> foundEntitiesShared set of entity IDs counted during a griefing check.
private Func<Vector3> getDirectionEither WorldSpaceDirection or LocalSpaceDirection, set from isWorldSpace.
private bool isBeingReplacedByPrefabOnCraftedIf true, placing spawns prefabToReplaceWith and destroys this pickup instead of sticking it.
private bool isWorldSpaceIf true, direction is in world space; otherwise it rotates with the object.
private LayerMask layerMaskLayers the raycast can hit.
private float maximumNormalHighest surface normal Y the pickup can be placed on.
private float minimumNormalLowest surface normal Y the pickup can be placed on. Both default to 1, so flat ground only.
private static Collider[] overlapShared buffer for the griefing overlap check (20 colliders).
private Pickup pickupThe pickup on this object.
private PooledObject pooledPreview effect currently shown, if any.
private NetworkPrefab prefabToReplaceWithPrefab spawned at the hit point when isBeingReplacedByPrefabOnCrafted is on.
private MethodSync syncNotPlacedMethod sync on SyncA for unplacing.
private MethodSyncStruct<RaycastCrafter.PlacedItem> syncPlaceMethod sync on SyncPlaced for placing.
private List<PooledObject> usingPooledAll preview effects taken from the pool and not yet released.

Properties​

MemberWhat it is
bool IsCrafted { get; private set; }True while the pickup is placed on a surface. Saved; loading as placed fires crafted on the server.

Methods​

MemberWhat it is
override void Initialize()Hooks the pickup's grab and release events, picks world or local direction and creates the syncs.
override void OnConnected(Player player)Sends the placed transform to a joining player if the pickup is placed.
protected virtual void OnSpawn(NetworkEntity entity)Virtual hook called on the new entity after ReplaceByPrefab spawns it. Empty here.
21 private and internal methods
MemberWhat it is
private static bool ColliderCountsTowardsGriefingLimit(Collider collider, HashSet<uint> foundEntitiyIdentifiers)True if the collider belongs to a network entity not already counted in the set.
private IEnumerator DelayToPlace() (iterator)One frame after release, raycasts and places or replaces the pickup; kicks the player if the spot is a griefing pile.
private void FinishPooled()Stops the surface preview particles and releases them after two seconds.
private void InteractionEnded(Interactable interactable, Interactor interactor)When the last hand lets go, places the pickup on the server (or locally if not replaced), else syncs not placed.
private void InteractionStarted(Interactable interactable, Interactor interactor)Shows the surface preview on clients, turns the pickup's physics sync back on and clears IsCrafted.
private static bool IsGriefingAction(RaycastHit hit)True when 10 or more distinct entities overlap within 0.1 m of the hit point.
private IEnumerator KillPooled(PooledObject pooled) (iterator)Coroutine that releases a preview effect after two seconds.
private Vector3 LocalSpaceDirection()Returns direction rotated by the object's rotation.
private IEnumerator LookForSurface() (iterator)Client coroutine that moves a preview effect to the raycast hit every frame while held.
private void OnDisable()Releases all preview effects back to the pool.
private void OnDrawGizmosSelected()Draws the raycast line in the editor.
private void OnValidate()Reselects world or local direction when the setting changes in play mode.
private RaycastCrafter.PlacedItem PlaceOnSurface(RaycastHit hit)Drops the pickup, moves it to the hit point facing the surface normal and freezes it. Fires crafted on the server.
private bool Raycast(out RaycastHit hit)Raycasts from the object along the direction and accepts the hit only if its normal's Y is between the min and max.
private RaycastCrafter.PlacedItem ReplaceByPrefab(RaycastHit hit)Spawns prefabToReplaceWith at the hit point facing the surface normal, frozen. The old pickup is destroyed after.
private void SwapCoroutine(IEnumerator to)Stops the running coroutine and starts a new one, if given.
private void SyncNotPlaced(IPlayer player, Stream stream)Method sync that marks the pickup as not placed and unfreezes it unless docked.
private void SyncPlaced(IPlayer player, Stream stream, RaycastCrafter.PlacedItem placedItem)Method sync with the placed entity, position and rotation. Clients freeze it in place and fire crafted.
private static bool TryGetColliderEntity(Collider overlappingCollider, out NetworkEntity entity)Gets the network entity on the collider's attached rigidbody, if any.
private static bool WillNotReachGriefingLimit(HashSet<uint> foundEntities, int overlapCount, int i)True if the colliders left can't bring the entity count up to 10.
private Vector3 WorldSpaceDirection()Returns direction as is.

Used in​

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