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.
- Listed in Items, inventory and logic
- Inherits or implements
MonoBehaviour,IGetRigidbody - Derived types
TriggerImpactTool - Nested types
OnCollidedHit,OnCollisionEnterEvent,OnGeneratedHit,ResetConditions - Documented in Impacts and hit detection
Constructors
| Member | What it is |
|---|---|
ImpactTool() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
33 private and internal fields
| Member | What it is |
|---|---|
private LayerMask blockableLayers | Layers whose collisions are checked for blocking. |
private Transform cachedTransform | Cached transform used by the position recorder. |
private UnityEvent collided | Unity event invoked on every collision. |
private HashSet<Rigidbody> collidedRigidBodies | Rigidbodies already handled this frame, so one collision isn't processed twice. Cleared next frame. |
private LocomotionController currentlyTrackingPlayer | Locomotion controller of the holder, whose movement is fed into the recorder. |
private float damage | Backing field for Damage: the base health damage of a hit. Default 1. |
private float dualHandedMultiplier | Efficiency multiplier when held with two or more hands. |
private NetworkEntity entity | Backing field for Entity. |
private bool forceIndexImpactors | Builds the indexed impactor list even when the tool has no network entity. |
private bool hasPickup | Backing field for HasPickup, baked from whether a pickup is in the parents. |
private UnityEvent hit | Unity event invoked on every successful hit. |
private Component[] hitDependants | Backing field for HitDependants: child components that get OnHit and OnCollided. |
private List<ImpactGenerator> indexedImpactors | Ordered impactors, so a hit can be synced as an impactor index. |
private bool isIgnoringInteractor | Set on release so the first contact with the last holder is ignored, until timeToSelfHit passes. |
private bool isIgnoringVelocityCheck | Backing field for IsIgnoringVelocityCheck. |
private bool isMedHandle | With a single hand, impactors whose pickup has BypassMed1h get full efficiency instead of the one hand multiplier. |
private Coroutine isRecentGrab | Coroutine running for 0.1 seconds after a grab; no hits are generated meanwhile. |
private Vector3 lengthAxis | Local axis along the tool's length, behind LengthAxis. |
private static Logger logger | NLog logger for this class. |
private ImpactorMap[] maps | Impactor maps on this object and its children, collider to impactors. |
private bool needsToBeProcessedInstantly | Backing field for NeedsToBeProcessedInstantly. |
private Pickup pickup | Backing field for Pickup, the pickup in this object's parents. |
private bool providesExperience | Backing field for ProvidesExperience. Default true. |
private PositionRecorder recorder | Backing field for Recorder, pooled from PickupTypeSettings. |
private BlockLevel requiredBlockLevel | Block level at or above which a block resets the recorder, when OnBlocked reset is set. |
private ImpactTool.ResetConditions resetConditions | When to reset the recorded swing: on a block, on a valid hit, or both. Default valid hit. |
private Rigidbody rigidbody | Backing field for Rigidbody. |
private static ContactPointStructure[] singleContact | Shared one element contact array put on the best hit. |
private float singleHandedMultiplier | Efficiency multiplier when held with one hand. |
private float timeToSelfHit | Seconds after release before the tool can hit its last holder. Default 1.5. |
private static UpdateModule updateModule | Shared fixed update module that runs UpdatePositions for all tools. |
private IUnityUpdateMethod updatePositions | This tool's registration in updateModule, or null when not recording. |
private bool updatesPosition | Whether any impactor needs the position recorder. |
Properties
| Member | What 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
Events
| Member | What it is |
|---|---|
event CollisionFilter CollisionFilter | Raised when another impact tool collides with this one. A handler can mark the hit consumed to cancel blocking. |
event ImpactTool.OnGeneratedHit OnBlocked | Raised on a tool when its swing is blocked by this tool. |
event ImpactTool.OnCollidedHit OnCollided | Raised on every collision, with whether it counted as a hit. |
event ImpactTool.OnCollisionEnterEvent OnCollisionEnterCalled | Raised when a held tool collides with something on a blockable layer. |
event ImpactTool.OnGeneratedHit OnHit | Raised when the tool lands a hit. Hit dependants are subscribed to it. |
static event ImpactTool.OnGeneratedHit OnHitAny | Static: raised for every hit by any impact tool. |
event Action<ImpactHitWithTool> OnHittingImpactDetector | Raised when a hit reaches an impact detector. Impact modifiers listen to it to change the hit. |
event Action Reseted | Raised when the position record is cleared, for example after a hit or a block. |
Methods
| Member | What 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
| Member | What 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.