Skip to main content

ImpactTool

A class in Root.Township, Alta.Impact.

Component on anything that can hit with its impactors (weapons, tools, arrows, crafted handles). Records its motion, turns collisions with an ImpactDetector into ImpactHitWithTools, handles blocking and wears durability. Runs wherever the physics happens; client-side hits are synced to the server by impactor index.

Constructors​

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

Fields​

33 private and internal fields
MemberWhat it is
private LayerMask blockableLayersLayers whose collisions are checked for blocking.
private Transform cachedTransformCached transform used by the position recorder.
private UnityEvent collidedUnity event invoked on every collision.
private HashSet<Rigidbody> collidedRigidBodiesRigidbodies already handled this frame, so one collision isn't processed twice. Cleared next frame.
private LocomotionController currentlyTrackingPlayerLocomotion controller of the holder, whose movement is fed into the recorder.
private float damageBacking field for Damage: the base health damage of a hit. Default 1.
private float dualHandedMultiplierEfficiency multiplier when held with two or more hands.
private NetworkEntity entityBacking field for Entity.
private bool forceIndexImpactorsBuilds the indexed impactor list even when the tool has no network entity.
private bool hasPickupBacking field for HasPickup, baked from whether a pickup is in the parents.
private UnityEvent hitUnity event invoked on every successful hit.
private Component[] hitDependantsBacking field for HitDependants: child components that get OnHit and OnCollided.
private List<ImpactGenerator> indexedImpactorsOrdered impactors, so a hit can be synced as an impactor index.
private bool isIgnoringInteractorSet on release so the first contact with the last holder is ignored, until timeToSelfHit passes.
private bool isIgnoringVelocityCheckBacking field for IsIgnoringVelocityCheck.
private bool isMedHandleWith a single hand, impactors whose pickup has BypassMed1h get full efficiency instead of the one hand multiplier.
private Coroutine isRecentGrabCoroutine running for 0.1 seconds after a grab; no hits are generated meanwhile.
private Vector3 lengthAxisLocal axis along the tool's length, behind LengthAxis.
private static Logger loggerNLog logger for this class.
private ImpactorMap[] mapsImpactor maps on this object and its children, collider to impactors.
private bool needsToBeProcessedInstantlyBacking field for NeedsToBeProcessedInstantly.
private Pickup pickupBacking field for Pickup, the pickup in this object's parents.
private bool providesExperienceBacking field for ProvidesExperience. Default true.
private PositionRecorder recorderBacking field for Recorder, pooled from PickupTypeSettings.
private BlockLevel requiredBlockLevelBlock level at or above which a block resets the recorder, when OnBlocked reset is set.
private ImpactTool.ResetConditions resetConditionsWhen to reset the recorded swing: on a block, on a valid hit, or both. Default valid hit.
private Rigidbody rigidbodyBacking field for Rigidbody.
private static ContactPointStructure[] singleContactShared one element contact array put on the best hit.
private float singleHandedMultiplierEfficiency multiplier when held with one hand.
private float timeToSelfHitSeconds after release before the tool can hit its last holder. Default 1.5.
private static UpdateModule updateModuleShared fixed update module that runs UpdatePositions for all tools.
private IUnityUpdateMethod updatePositionsThis tool's registration in updateModule, or null when not recording.
private bool updatesPositionWhether any impactor needs the position recorder.

Properties​

MemberWhat it is
HashSet<ImpactChannel> Channels { get; }All impact channels of the registered impactors, including those from crafted child parts.
float Damage { get; }Base damage of the tool, set on the prefab. Impactor modifiers and stats multiply it.
NetworkEntity Entity { get; set; }The network entity the tool is on.
bool HasImpactors { get; private set; }Whether any impactors are registered.
bool HasPickup { get; }Whether the tool has a Pickup in its parents.
Component[] HitDependants { get; }Child components that implement IToolHitDependant and get OnHit and OnCollided calls.
Dictionary<Collider, HashSet<ImpactGenerator>> ImpactorsPerCollider { get; }Map from each collider to the impactors on it.
bool IsIgnoringVelocityCheck { get; }When true, detectors accept hits from this tool even when it is barely moving.
Vector3 LastPosition { get; }The tool's current position.
Quaternion LastRotatation { get; }The tool's current rotation (name misspelt in the game).
Vector3 LengthAxis { get; }The tool's length direction in world space, from the prefab's local axis.
float MaxDamage { get; }Damage times the highest health damage modifier among its impactors.
bool NeedsToBeProcessedInstantly { get; }When true, detectors resolve this tool's hits at once instead of through the delayed resolver.
float OneOffMultiplier { get; set; }Damage multiplier for the next hit only. Reset to 1 after a hit lands.
Pickup Pickup { get; }The pickup this tool belongs to, if any.
Player PlayerOwner { get; set; }The player credited with hits when nobody holds the tool, such as a fired arrow.
PlayerController PlayerWielder { get; }The last player to hold the tool's pickup, or null if it has no pickup.
bool ProvidesExperience { get; }Whether hits from this tool can give progression experience.
PositionRecorder Recorder { get; }Records the tool's recent positions so impactors can work out swing direction and speed.
Rigidbody Rigidbody { get; }The tool's own rigidbody.
virtual Vector3 Velocity { get; }Velocity of the pickup's rigidbody, or the tool's own if it has no pickup.
NetworkEntity Wielder { get; }The root entity holding the tool (last interactor), or the tool's own root if it has no pickup.
1 private and internal property
MemberWhat it is
private Coroutine CallCoroutine { get; set; }Private coroutine slot that nothing in this class sets.

Events​

MemberWhat it is
event CollisionFilter CollisionFilterRaised when another impact tool collides with this one. A handler can mark the hit consumed to cancel blocking.
event ImpactTool.OnGeneratedHit OnBlockedRaised on a tool when its swing is blocked by this tool.
event ImpactTool.OnCollidedHit OnCollidedRaised on every collision, with whether it counted as a hit.
event ImpactTool.OnCollisionEnterEvent OnCollisionEnterCalledRaised when a held tool collides with something on a blockable layer.
event ImpactTool.OnGeneratedHit OnHitRaised when the tool lands a hit. Hit dependants are subscribed to it.
static event ImpactTool.OnGeneratedHit OnHitAnyStatic: raised for every hit by any impact tool.
event Action<ImpactHitWithTool> OnHittingImpactDetectorRaised when a hit reaches an impact detector. Impact modifiers listen to it to change the hit.
event Action ResetedRaised when the position record is cleared, for example after a hit or a block.

Methods​

MemberWhat it is
void ClearHitEvents()Clears OnHit and OnCollided, then re-adds only the tool's own hit dependants.
void DeregisterChildImpactTool(IEnumerable<Component> hitDependants, ICollection<ImpactorMap> maps)Removes a crafted child part's hit dependants and impactors from this tool. Called when a part is taken off.
bool GenerateHit(out ImpactHitWithTool bestHit, ImpactDetector detector, ContactPointStructure contact, Vector3 relativeVelocity, ImpactChannel[] channels, bool isDamaging = true, bool isOverridingEffects = true, BlockLevel blockLevel = 4)Works out the best hit from one contact across the collider's impactors, then applies effects, haptics and durability loss. Ignored for 0.1 s after a grab.
ImpactHitWithTool GenerateImpactOnDetector(ImpactDetector detector, int impactorIndex, float effectiveness, int channelIndex, Vector3 point, Vector3 normal)Rebuilds and fires a hit from synced values (impactor and channel index), wearing durability. Used when a client-side hit arrives.
bool GetCurrentPlayerWielder(out PlayerController wielder)True with the player holding the tool's pickup, if it is held.
bool GetFullTriggerHit(ContactPointStructure contact, ImpactChannel[] channels, out ImpactHitWithTool bestHit)Best hit across every impactor for a trigger contact, with hand-count and damage stat multipliers. False if none match.
void GetHandCountEfficiencyMultiplier(ref float efficiencyMultiplier, ref float damageMultiplier)Sets efficiency to the single or dual handed multiplier by how many hands hold the pickup. Leaves damage unchanged.
float GetImpactDamageOn(ImpactDetector detector, int impactorIndex, float effectiveness, int channelIndex)Health damage a synced hit with these values would do on a detector, without applying it.
int GetImpactorIndex(ImpactGenerator impactor)Index of an impactor in this tool's list, for syncing hits. -1 with an error if it is not on this tool.
float GetTriggerHit(ContactPointStructure contact, ImpactChannel[] channels, ImpactType type)Best efficiency of the touched collider's impactors of a given type and channel, or -1 if none.
void HitImpactDetector(ImpactHitWithTool hit)Raises OnHittingImpactDetector for a hit. Called from ImpactHitWithTool.Hit.
bool IsWieldedByLocalPlayer()True if the pickup is held and its last player interactor is the local player.
void RegisterChildImpactTool(Component[] hitDependants, HashSet<ImpactorMap> maps)Adds a crafted child part's hit dependants and impactors to this tool. Called when a part is attached.
void StartRecordingPosition()Starts recording positions if any impactor needs them.
void UpdateRigidbody()Re-reads the rigidbody from the game object.
protected virtual void Awake()Gets a position recorder, hooks grab and release on the pickup and registers the impactors found in children.
protected void Hit(ImpactHitWithTool hit)Protected: fires the inspector hit event, OnHit and the static OnHitAny.
17 private and internal methods
MemberWhat it is
private void DeregisterPlayer()Stops feeding the tracked player's movement and turning into the position recorder.
private bool GetOverlapChannel(List<ImpactChannel> impactorChannels, ImpactChannel[] detectorChannels, out ImpactChannel overlap)Finds the first impactor channel that is also in the detector's channels.
private void Grabbed(Interactable interactable, Interactor interactor)Handler for the pickup's InteractionStarted: blocks hits for 0.1 seconds, starts position recording and tracks the holder's locomotion.
private void Haptic(AnimationCurve haptic)Client: plays a haptic curve on every hand holding the pickup.
private void OnCollisionEnter(Collision collision)Handles physical hits against other held items and tools: works out blocking, applies blocking skills, impact modifiers and durability, and raises OnCollided.
private void OnDestroy()Returns the position recorder to the pool and stops the position updates.
private void OnDrawGizmosSelected()Draws the length axis in the editor.
private IEnumerator RecentGrab() (iterator)Coroutine that clears the recent grab flag after 0.1 seconds.
private void RegisterImpactorChannels(ImpactGenerator impactor)Adds an impactor's channels to Channels.
private void Released(Interactable interactable, Interactor interactor)Handler for the pickup's InteractionEnded: lets the tool hit its own wielder again after timeToSelfHit, and stops tracking the player once nobody holds it.
private void Reset()Clears the position recorder and raises Reseted.
private void SetupImpactors()Called on awake: registers this tool with every impactor in its maps, collects channels and starts position updates if any impactor needs them.
private IEnumerator TimeToHurtSelf() (iterator)Coroutine that waits timeToSelfHit seconds, then stops ignoring the last holder.
private void UndockWakeUp(Interactor interactor, Pickup pickup, PickupDock dock)Handler for the pickup's Undocked: starts position updates again.
private void UpdateIndexedImpactors()Rebuilds the ordered impactor list used to sync hits by index, including impactors on crafted child parts.
private void UpdateIndexedImpactorsOnCraftingPart(CraftingPart part)Adds the impactors of every part connected to a crafting part, recursing down the tree.
private void UpdatePositions()Fixed update: records the tool's position, or stops and resets once it is asleep and not held, or is a crafted child.

Used in​

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

AfkTimer.ResetWithHit, ChiselingTool.UpdateTool, CraftingPart.impactTool, CraftingPart.ImpactTool, QuickDrawApplication.Proc, QuickDrawApplication.tool, QuickDrawApplication.ValidateHit, BlockedTrigger.Blocked, ColliderModifierSettings.IsDamaging, ContactProjectile.OnCollided, ContactProjectile.OnHit, CraftableImpactDetectorBase.RegisteredTool, CraftableImpactDetectorBase.UpdateTool, DamageMultiplierCollider.IsDamaging, DelayedImpactResolver.frameHits, DelayedImpactResolver.Hit, DelayedImpactResolver.ProcessImmediate, DelayedImpactResolver.ImpactDetectorHit.ImpactDetectorHit, DelayedImpactResolver.ImpactDetectorHit.impactTool, DirectionalMultiplierCollider.IsDamaging, ImpactDetector.hitCooldowns, ImpactDetector.IsForcedClientSideImpact, ImpactDetector.IsImpactClientSideOnly, ImpactDetectorColliderModifier.IsDamaging, ImpactGenerator.DeRegisterTool, ImpactGenerator.RegisteredTool, ImpactGenerator.RegisterTool, ImpactHitWithTool.ImpactHitWithTool, ImpactHitWithTool.Tool, ImpactModifier.trackedTool, Impactor.impactTool, ImpactTool.OnCollidedHit.BeginInvoke, ImpactTool.OnCollidedHit.Invoke, ImpactTool.OnGeneratedHit.BeginInvoke, ImpactTool.OnGeneratedHit.Invoke, ImpactTypeFilter.ValidateImpacTool, IToolHitDependant.OnCollided, IToolHitDependant.OnHit, NonDamagingCollider.IsDamaging, TriggerImpactDetector.GenerateHit, SeedSpray.Seed.tool, NetworkEntity.commonImpactTool, NetworkEntity.CommonImpactTool, Arrow.impactTool, Arrow.ImpactTool, Arrow.OnCollided, Arrow.OnHit, BreakOnImpact.OnCollided, BreakOnImpact.OnHit, DiggingImpactor.currentTool, DiggingImpactor.OnCollided, DiggingImpactor.OnHit, DiggingImpactor.RegisterTool, DynamiteStrike.Collided, Embeddable.OnCollided, Embeddable.OnHit, ExtraArrowUpdater.arrowImpactTool, HeartCall.modifiers, HeartCall.Modify, HeldPickupHandler.Tool and 10 more.