Skip to main content

Impacts and hit detection

The impact system is in Alta.Impact in Root.Township. It covers weapons hitting creatures, pickaxes on rock, axes on trees, hammers on metal and blocks with shields. All of it runs on Unity's main thread.

The parts​

TypeWhere it isWhat it does
ImpactToolOn the rigidbody of a weapon or tool (and on creature attacks).Turns a collision into a hit. Holds the base Damage.
ImpactGenerator (abstract), Impactor, FixedImpactorChildren of the tool: a blade edge, a hammer face, a point.Each has a type (Pierce, Slash, Blunt, Caustic, Poison, Explosive, Spiritual), channels, a hardness and damage modifiers. Works out how good the swing was.
ImpactDetectorOn anything that can be hit: creature body parts, rocks, trees, players.Receives hits, applies the cooldown, sends hits to the server, raises Hit.
ImpactChannelA ScriptableObject.A hit only counts if the impactor and the detector share a channel (so a pickaxe mines and a sword doesn't).
ImpactHit, ImpactHitWithToolPlain classes, subclasses of DamageData.One hit: damage, effectiveness, point, who did it.
DelayedImpactResolverStatic.Collects a frame's contacts per tool and resolves the best one in LateUpdate.
ImpactDetectorCentralOn entities with several detectors.Sends their hits in one message, with a detector index.
HealthObjectNext to the detectors.Turns hits into damage. See Health and damage.

What counts as a hit​

When a tool's collider touches a detector (ImpactDetector.OnCollisionEnter):

  1. The tool must have a wielder, and unless it ignores the velocity check, it must be moving at more than about 0.32 m/s (squared speed 0.1).
  2. The side check (next section) decides whether this machine works it out.
  3. The contact is queued in DelayedImpactResolver (or handled at once if the tool says NeedsToBeProcessedInstantly). Once a frame, for each tool, every contact is scored with ImpactTool.GenerateHit and the most effective one wins.
  4. GenerateHit refuses hits in the 0.1 s after the tool was grabbed, contacts on colliders that aren't one of its impactors, and impactors with no channel in common with the detector. If the detector is harder than the impactor, the damage is multiplied by 0 and you get the "ineffective" effect instead.
  5. Effectiveness (0 to 1) comes from how far the impactor moved over its recent positions (the tool keeps the last 20, PickupTypeSettings.ImpactorArraySize) and how well the movement lines up with the edge, times a one-hand or two-hand multiplier on the tool and any immunity group multipliers. The player's own walking doesn't count: the tool's position recorder takes the body's movement out.
  6. The hit goes to ImpactDetector.OnToolHit, which ignores the same tool on the same detector again within PickupTypeSettings.HitCooldown (0.25 s in the shipped asset).
  7. The detector raises Hit, and its HealthObject takes damage (on the server).

After a tool is let go, it can't hit the player who last held it for timeToSelfHit (1.5 s by default), so you don't hurt yourself throwing.

Who works it out​

This is the part that matters for server mods.

HitWorked out byThen
A player's held tool on a creature, tree, rock or anything that isn't a playerThe wielder's clientIt sends SyncImpact to the server. The server skips the collision itself.
A tool not held by a player (a creature's attack) on a playerThe victim's clientIt sends SyncImpact.
A player's held tool on another playerServerNo message from clients.
Anything else (an unheld tool hitting a creature, for example)Server

Clients don't stop there: every client runs the hits it sees through the same code, for sounds and effects, but only the cases above send anything, and only the server applies damage.

SyncImpact (entity message type SyncImpact) carries the tool's entity id, the impactor index (0 to 63), the channel index (0 to 7), the effectiveness, the normal, and the point relative to the detector (within 10 m, in 1 cm steps).

When the server gets one, ImpactDetector.SyncImpact checks only that:

  • a hit on a player was sent by that player,
  • the tool's pickup was last held by the sender (tools with no pickup aren't checked).

It doesn't check distance, line of sight, speed, hardness or channel, and it takes the effectiveness as sent. Then ImpactTool.GenerateImpactOnDetector rebuilds the hit and it goes through OnToolHit (so the cooldown applies) to the health object.

note

From the code, a hit rebuilt from SyncImpact uses the tool's Damage and the sent effectiveness, but not the wielder's damage stat, which GenerateHit applies on the client. The client's own numbers (in its damage popup) can be higher than what the server deals.

ImpactTool​

Alta.Impact.ImpactTool, a MonoBehaviour. Get it with GetComponent<ImpactTool>() on the item, or entity.CommonImpactTool.

MemberWhat it doesNotes
float Damage { get; }Base damage.
float MaxDamage { get; }Damage times the best impactor's health modifier.
Pickup Pickup { get; } / bool HasPickup { get; }The item it belongs to.
NetworkEntity Wielder { get; }The last holder's entity, or the tool's own root if it has no pickup (a creature).
PlayerController PlayerWielder { get; }The last player holder.
Player PlayerOwner { get; set; }A player credited for hits when nobody holds it (projectiles).
virtual Vector3 Velocity { get; }The rigidbody's velocity.
HashSet<ImpactChannel> Channels { get; }Channels of all its impactors.
float OneOffMultiplier { get; set; }A damage multiplier for the next hit only. Reset after a hit.
bool GenerateHit(out ImpactHitWithTool, ImpactDetector, ContactPointStructure, Vector3 relativeVelocity, ImpactChannel[], bool isDamaging = true, bool isOverridingEffects = true, BlockLevel = BlockEverything)Scores a contact.Also wears the tool down.
ImpactHitWithTool GenerateImpactOnDetector(ImpactDetector, int impactorIndex, float effectiveness, int channelIndex, Vector3 point, Vector3 normal)Rebuilds a reported hit.Server, from SyncImpact.
float GetImpactDamageOn(ImpactDetector, int impactorIndex, float effectiveness, int channelIndex)What a hit would deal.
bool GetCurrentPlayerWielder(out PlayerController)The player holding it now.
bool IsWieldedByLocalPlayer()
event OnGeneratedHit OnHitA hit was made with this tool.delegate void OnGeneratedHit(ImpactHitWithTool, ImpactTool)
static event OnGeneratedHit OnHitAnyA hit was made with any tool.
event OnGeneratedHit OnBlockedThis tool, on a held item, blocked another tool's swing.Raised on the blocker's ImpactTool with the striker's hit; the tool argument is the blocker.
event OnCollidedHit OnCollidedAny collision, hit or not.delegate void OnCollidedHit(Collision, bool isHitting, ImpactTool)
event CollisionFilter CollisionFilterLets this tool swallow a block collision.
event Action<ImpactHitWithTool> OnHittingImpactDetectorThe hit reached a detector.

Blocking: when a held tool hits another item (not a detector), the struck item's Pickup.BlockLevel decides. Shields and weapons that block take durability damage equal to the hit's damage, and if the blocking item has an ImpactTool, that tool's OnBlocked fires (not the striker's).

ImpactDetector​

Alta.Impact.ImpactDetector, a MonoBehaviour and INetworkEntityBehaviour.

MemberWhat it doesNotes
HealthObject HealthTarget { get; set; } / bool HasHealth { get; }The health it feeds. Set by the HealthObject in Awake.
ImpactChannel[] Channels { get; }What can hit it.
uint HardnessLevel { get; set; }Tools softer than this do nothing.Set from the material.
ArmorDefinition CurrentArmor { get; }Armour worn on this body part, if any.
ImpactDetectorSettings Settings { get; set; }Effects, XP and durability rules.
event ImpactHandler HitA hit landed. delegate void ImpactHandler(ImpactHit)Fires on every machine that resolved it, server included. Check the side before acting.
event ImpactHandler ParticleHitA particle impactor (fire breath, sprays) hit it.
void OnToolHit(ImpactHitWithTool)Applies the cooldown, sends to the server if this machine worked it out, raises Hit.
void OnImpactHit(ImpactHit)Raises Hit directly.
void SyncImpact(IPlayer, Stream, ImpactSyncArguments)The network handler.Public, but the arguments are read inside, so a prefix sees them unread.

ImpactHit and ImpactHitWithTool​

ImpactHit : DamageData, with Source always DamageSource.Impact.

MemberWhat it does
float HealthDamage { get; }BaseHealthDamage × Effectiveness × DamageMultiplier × Multipliers × HealthMultiplier, or 0 if not valid. Worked out each time you read it. BaseHealthDamage is the impactor's HealthDamageModifier: its ImpactorValues number times the impactor's own Multipliers (the material's damage multiplier).
float CrippleDamage { get; }Same with the cripple values.
static float DamageMultiplier { get; }The server's DamageX setting on a server, 1 on a client.
float Effectiveness { get; set; } / bool IsValid { get; }0 to 1; valid when above 0.
float Multipliers, HealthMultiplier, CripplingMultiplier { get; set; }Change these to change the damage.
ImpactType ImpactType { get; }Pierce, Slash and so on.
ImpactDetector ImpactDetector { get; set; }What was hit.
Collider Struck { get; set; }, Vector3 Point, Normal, DirectionWhere.
NetworkEntity Wielder { get; }, PlayerController PlayerWielder { get; }Who.

ImpactHitWithTool adds Tool, Impactor, FirstChannel, Start, End, Distance, HittingPlayer (the player to credit, or null), WasByPlayer, DurabilityLostMultiplier and Hit(), which sends it to its detector.

Asset values​

Asset values (build main-1.7.2.1.42203). Items and materials lists every item with its material and durability; impactor values aren't in the reference tables, so the ones below were read from the prefabs.

ImpactorValues​

Each impactor points at an ImpactorValues asset:

FieldWhat it does
typeThe ImpactType (0 Pierce, 1 Slash, 2 Blunt, 3 Caustic, 4 Poison, 5 Explosive, 6 Spiritual).
healthDamageModifier, crippleDamageModifierDamage per unit of tool damage, before effectiveness.
edgeLengthLength of the edge along the impactor's up axis; -1 for a point.
minimumFollow, maximumFollowEffectiveness runs from 0 to 1 as the farthest recorded position is between these distances (metres) from the contact.
directionPowerThe swing's alignment with the impactor's forward axis is raised to this power, so higher means stricter.
channelsWhat it can hit.

Hardness comes from ImpactorSettings.hardnessLevel, overridden by the item's material.

Some weapons​

PrefabImpactTool.damageOne hand / two handsMain impactorHealth / cripple modifierFollow min to maxdirectionPower
Short Sword11 / 1.4Slash, edge 0.7 m1 / 0.50.6 to 2.1 m0.8
Short Sword (stab)Pierce, point0.75 / 0.3750.4 to 1 m2.5
Katana10.8 / 1.1Slash, edge 0.95 m1.3 / 0.650.4 to 2.1 m2
Rusty Greataxe10.3 / 1Slash0.75 / 0.50.7 to 2.2 m0.45
Pick Axe101 / 1Pierce, Mining channel1 / 10.35 to 0.7 m1
Arrow (Damage impactor)11 / 1Pierce, ignores the speed check0.1 / 0.10.3 to 0.6 m2
Arrow (Damage (Archery) impactor)Pierce0.6 / 0.62.5 to 9.5 m2
Wyrm (each scythe)0.5Pierce, FixedImpactor1 / 1none (fixed)0
Spriggull1Blunt0.5 / 0.20.35 to 1.35 m1

An arrow has two impactors. Going by its name and its long follow distances, Damage (Archery) is probably the one that counts for bow shots; that wasn't traced in the code.

Every tool here has timeToSelfHit 1.5 s and requiredBlockLevel BlockPhysicalOnly. Swords and the greataxe block at BlockPhysicalOnly; bags, the pick and arrows can't block (CantBlock). A FixedImpactor reports effectiveness 1 while charged, so a Wyrm's scythe hit is 0.5 × 1 before the target's multipliers (a FixedImpactor with a damage stat uses that stat as its multiplier, so a stronger creature hits harder).

So a perfect two-handed Short Sword slash deals 1 × 1 × 1 (effectiveness 1.4 is clamped to 1) health on a server with DamageX 1.

Channels and armour​

The 10 ImpactChannel assets: Combat Impact Channel, Mining Impact, Wood Cutting, Explosive, Chiselling, Digging Impact Channel, Forging, Nailing Impact Channel, Flint Impact Flint, Flint Impact Other. Player bodies, creatures and breakable boxes listen on Combat and Explosive; boulders on Mining and Explosive.

ArmorDefinition multipliers by type:

ArmourPierceSlashBluntCausticExplosive
MetalArmor0.50.40.90.60.4
LeatherArmor0.70.70.85
MushroomArmor0.70.70.850.5
GoteraArmor0.90.80.60.5
WyrmArmor10.80.60.1
ObjectArmor10.80.70.7

Types not listed are taken at full damage.

Hooking it​

See every hit this machine resolves:

using Alta.Impact;
using MelonLoader;

public class HitLogger : MelonMod
{
public override void OnInitializeMelon()
{
ImpactTool.OnHitAny += OnHit;
}

static void OnHit(ImpactHitWithTool hit, ImpactTool tool)
{
if (!NetworkSceneManager.IsServer) return;
string who = hit.HittingPlayer?.UserInfo.Username ?? tool.Wielder?.OriginalName ?? "?";
HealthObject target = hit.ImpactDetector?.HealthTarget;
MelonLogger.Msg($"{who} hit {target?.Entity.OriginalName ?? "something"} for {hit.HealthDamage:0.0} ({hit.ImpactType}, {hit.Effectiveness:0.00})");
}
}

On a server, OnHitAny fires for PvP hits it resolved and for hits clients reported. On a client it fires for every hit that client resolves, including the ones it only shows for effects; check tool.IsWieldedByLocalPlayer() for the local player's own.

Refuse reported hits from too far away, on the server:

using Alta.Impact;
using HarmonyLib;

[HarmonyPatch(typeof(ImpactTool), nameof(ImpactTool.GenerateImpactOnDetector))]
static class RejectFarHits
{
const float MaxDistance = 5f;

static void Prefix(ImpactTool __instance, ImpactDetector detector, ref float effectiveness)
{
if (!NetworkSceneManager.IsServer || detector == null) return;
if ((__instance.transform.position - detector.transform.position).sqrMagnitude > MaxDistance * MaxDistance)
effectiveness = 0f;
}
}

The detector's transform can be some way from where a big creature was struck, so pick the limit with care.

Change the damage of reported hits:

using Alta.Impact;
using HarmonyLib;

[HarmonyPatch(typeof(ImpactTool), nameof(ImpactTool.GenerateImpactOnDetector))]
static class ReportedHitDamage
{
static void Postfix(ImpactHitWithTool __result)
{
if (NetworkSceneManager.IsServer && __result.HittingPlayer?.UserInfo.Username == "Placeholder")
__result.Multipliers *= 2f;
}
}

Things to watch:

  • Don't skip GenerateImpactOnDetector. SyncImpact calls .Hit() on what it returns, so a prefix returning false makes it throw. Set effectiveness to 0 instead.
  • Order inside GenerateImpactOnDetector. It raises OnHit and OnHitAny and wears the tool down before it returns, so those see the hit before your postfix changes it. HealthDamage is read again when the damage lands, so the postfix still counts.
  • PvP hits don't come through SyncImpact. The server resolves them through DelayedImpactResolver and GenerateHit. To change PvP damage, use PlayerHealth.ReceiveDamage or the server's PvP multipliers (see Health and damage).
  • DelayedImpactResolver.Update must not throw. It's private and registered once. If it throws outside its own try block (a queued hit whose tool has no entity), the update loop drops it and hits stop doing damage until a restart. The Steady mod patches it with a prefix that drops bad entries and a finalizer that keeps it alive.
  • GenerateHit runs for every contact of every moving tool. Keep patches on it cheap.
  • ImpactDetector.OnCollisionEnter is a Unity message, called by physics.

Other types​

TypeWhat it is
ImpactorSettings, ImpactorValuesImpactor data: hardness, filters, effects, type, damage modifiers.
ImpactDetectorSettingsDetector data: hit effects, whether it gives XP, whether it wears tools.
ImpactImmunityGroupA multiplier when an impactor and a detector share a group.
ArmorDefinition, EquipmentReferenceArmour: per-type multipliers on a body part's detector.
ColliderModifierSettings, NonDamagingCollider, DamageMultiplierCollider, DirectionalMultiplierColliderPer-collider tweaks on a detector (a weak spot, a part that can't be damaged).
ParticleImpactorParticle damage (ParticleHit).
TriggerImpactDetector, TriggerImpactTool, CutableObjectTrigger-based hits, like cutting.
SparkImpactDetector, CraftableImpactDetectorSmithing and crafting hits.
ContactProjectile, NetworkContactProjectileProjectiles.
PickupBreakOnImpactBreaks an item on a hard hit.
MiningSettingsGlobal haptics and strike sound settings.
ImpactSystemHitManagerDebug: sends the server's hits to subscribed clients.

See also​