Skip to main content

Embeddable

A class in Root.Township, the global namespace.

Lets a pointed item (arrow, knife, spear) stick into what it hits and parents it there, saved across restarts. The server decides when it embeds; clients follow a synced message. Get it with GetComponent<Embeddable>() on the item.

Constructors​

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

Fields​

24 private and internal fields
MemberWhat it is
private float breakForceSerialized force (10000). Not read in this build.
private bool btnClearGizmosInspector button that runs ClearGizmos.
private bool btnUnEmbedInspector button labelled "UnEmbed".
private float distanceToCheckForWedgeRaycast length for finding the embed spot: tip distance times raycastDistanceMultiplier.
private EmbeddableEvent embeddedEvent invoked on embed. The Embedded event adds to it.
private bool isAllowedToEmbedBacking field for IsAllowedToEmbed.
private PlayerController lastEmbeddedPlayerThe player the object is stuck in, watched so it can unembed when they leave.
private static Logger loggerNLog logger for debug messages.
private float maxEmbedAngleMinimum alignment (a dot product set from an angle) needed between tip and surface to embed.
private float minHitEffectivenessToWedgeLowest hit effectiveness, after the surface multiplier, that embeds (0.3 by default).
private float minSquaredWedgingVelocityLowest squared impact speed that can embed (10 by default).
private Vector3 negativeTipOffsetLocal offset from the tip back to the origin.
private NetworkRigidbody networkRigidbodyThe object's network rigidbody, turned off while embedded.
private float nonEmbeddingHitDragVelocity multiplier applied when a hit is at the wrong angle to embed (0.5 by default).
private IEnumerable<Collider> othersCollidersColliders of the struck entity whose collisions are ignored while embedded.
private float raycastDistanceMultiplierScales the tip distance to get the wedge raycast length (1.5 by default).
private static RaycastHit[] raycastHitsShared buffer for the wedge raycast hits.
private Rigidbody rigidbodyThe object's Rigidbody.
private MethodSyncClass<EmbedArguments> syncEmbedMethod sync made in Initialize for SyncEmbed, also sent to players on connect.
private Transform tipPointTransform at the tip that goes in first.
private EmbeddableEvent unembeddedEvent invoked on unembed. The Unembedded event adds to it.
private float wedgeMaximumPercentageHow deep a perfect hit sinks, as a share of the tip length (0.1 by default).
private float wedgeMinimumPercentageHow deep the weakest embedding hit sinks, as a share of the tip length.
private LayerMask wedgingLayersLayers the wedge raycast hits.

Properties​

MemberWhat it is
IEnumerable<Collider> Colliders { get; }The item's pickup colliders, or empty when the entity has no Pickup. Used to ignore collisions with what it is stuck in.
NetworkEntity EmbeddedEntity { get; private set; }The entity the item is stuck in, or null when stuck in the static world or not embedded.
Rigidbody EmbeddedInto { get; private set; }The rigidbody the item is stuck in, or null when stuck in the static world or not embedded.
int EmbeddedRigidbodyIndex { get; private set; }Index of the struck rigidbody in the target's EmbeddableManager list, or -1 when it has none or is not embedded.
bool HasPickup { get; }Whether the entity has a common Pickup.
bool IsAllowedToEmbed { get; set; }Turns embedding on or off for this item. Set it on the server.
bool IsEmbedded { get; private set; }Whether the item is currently stuck in something.
1 private and internal property
MemberWhat it is
private Vector3 ToTipDirection { get; }World direction from the object's origin to its tip.

Events​

MemberWhat it is
event RefBoolDelegate CheckEmbedRaised before an embed; set the ref bool to false to refuse it. Pickups add a handler that refuses while held or docked.
event Action<Embeddable> EmbeddedRaised on both sides when the item sticks into something.
event Action<Embeddable> UnembeddedRaised on both sides when the item comes free.

Methods​

MemberWhat it is
void ExternalCollision(Collider collider, Vector3 hitPoint, Vector3 hitNormal)Lets other code (such as EmbeddableRaycastProjectile) report a collision; embeds if angle, speed and surface allow it.
override void Initialize()Sets up the synced embed message (FixedJoint entity message type).
override void OnConnected(Player player)Server: sends the current embed state to a newly connected player.
void UnEmbed()Frees the item, unparents it and restores physics. Only syncs to clients when called on the server.
32 private and internal methods
MemberWhat it is
private void Awake()Works out the tip offsets, unembeds when the pickup is grabbed, blocks embedding while held or docked, and unembeds on destroy.
private void ChildToStruckRigidbody(NetworkEntity otherEntity, Transform parent)Makes the struck entity the chunking parent, saves as its child, and parents the transform to the struck body.
private void ClearGizmos()Empty. Run by the "Clear Gizmos" inspector button.
private void DisableCollisionsWithOther(NetworkEntity other)Finds the struck entity's colliders (player body, creature, pickup or collider group) and ignores collisions with them.
private void Embed(Rigidbody struckRigidBody, Vector3 embedPosition, Quaternion embedRotation)Server: sticks the object at a position, attaches it to the struck entity if any, fires Embedded and syncs to clients.
private void EnforcePickupElibility(ref bool isValid)Embed check that fails while the object's pickup is held or docked.
private bool EnsureTipPoint()Inspector fix: uses the object's own transform as the tip if none is set.
private bool FindBestHit(int numberOfHits, RaycastHit[] raycastHits, out RaycastHit bestHit)Picks the closest raycast hit that isn't the object itself or the throwing player's body.
private float GetBestHitAngle(ContactPointStructure[] contacts)Best dot product between the tip direction and the contact normals, used against maxEmbedAngle.
private void GetBestHitLocation(float effectiveness, out Quaternion lastRotation, out Vector3 embedPosition)Raycasts along the tip to find where to sit, sunk in deeper the more effective the hit was.
private EmbedArguments GetSyncArgs()Builds the embed sync data: state, position (local if in a body), rotation, struck entity, body index and scale.
private void HandleDestroyed(NetworkEntity entity, bool isUnload)Unembeds when the entity is destroyed, but not when it's only unloading.
private void HandleEmbedInGlobalSpace(Vector3 embedPosition, Quaternion embedRotation)Makes the body kinematic and places it at a world position and rotation.
private void HandleEmbedInLocalSpace(Vector3 localPosition, Quaternion localRotation, Transform parent)Makes the body kinematic and parents it at a local position and rotation.
private void HandleStruckCollider(float hitEffectiveness, Collider struckCollider, ContactPointStructure[] contactPointStructures, Vector3 relativeVelocity)Embeds if allowed, fast enough, and the surface and pickup checks pass.
private void HandleStruckRigidbodyWithCorrectAngleAndVelocity(float hitEffectiveness, Collider struckCollider, Rigidbody struckRigidBody, float embeddMultiplier)Embeds when hit effectiveness times the surface multiplier reaches minHitEffectivenessToWedge.
private void HandleUnEmbedOnClient()Client: makes the body physical again, moves it to the spawn folder and turns collisions back on.
private void IgnoreCollisions(bool isIgnoring)Turns collisions between this object's colliders and the struck entity's colliders off or on.
private static bool IsRaycastedRigidbodyFromTheThrowingPlayer(PlayerCharacter playerCharacter, RaycastHit raycastHit)True when a raycast hit is one of the throwing player's own body rigidbodies.
private bool IsValidEmbed()False if already embedded, embedding is off, or a CheckEmbed handler says no.
private bool IsValidPickup(Collider struckCollider)When the global settings restrict it, only lets the object stick into pickups with the allowed tag.
private bool IsValidSurface(Collider struckCollider, ContactPointStructure[] contactPointStructures, ref float embedMultiplier)Checks the surface's embed multiplier and, if it needs alignment, the hit angle. A bad angle slows the object.
private void Alta.Impact.IToolHitDependant.OnCollided(Collision collision, bool isHitting, ImpactTool tool)Server: on a plain collision (not a tool hit), tries to embed with full effectiveness.
private void OnDrawGizmosSelected()Editor: draws the wedge depth range and the raycast line.
private void Alta.Impact.IToolHitDependant.OnHit(ImpactHitWithTool hit, ImpactTool tool)Server: on a tool hit, tries to embed using the hit's effectiveness.
private void ReenableCollisions()Turns collisions with the struck entity back on, unless that player is now holding this object.
private void SetupTipOffsets()Stores the tip offset and the wedge raycast distance from the tip point.
private void SyncEmbed(IPlayer player, Stream stream, EmbedArguments args)Method sync for embed state. Clients embed or unembed to match and fire the events.
private bool TryGetStruckObject(Rigidbody struckRigidBody, out NetworkEntity struckEntity, out int rigidbodyIndex, out EmbeddableManager manager)Finds the struck network entity and, through an EmbeddableManager, the index of the body hit.
private void UnChild()Unparents the object, restores its save mode and chunk, and turns collisions back on.
private void UnEmbedOnInteract(Interactable arg1, Interactor arg2)Unembeds when someone grabs the object.
private void UnEmbedOnPlayerLeave(PlayerController player)Unembeds when the player the object is stuck in is about to leave.

Used in​

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