Class DamageHandler
Handles vehicle collision damage detection, mesh deformation, and performance degradation based on impact forces and collision events.
Namespace: NWH.VehiclePhysics2.Damage
Assembly: NWH.VehiclePhysics2.dll
Syntax
[RequireComponent(typeof(VehicleController))]
[RequireComponent(typeof(Rigidbody))]
public class DamageHandler : MonoBehaviour
Remarks
On collision, deforms instanced copies of the vehicle's meshes near the impact and, unless visualOnly is set, accumulates wear on the wheels, engine and transmission from the impact impulse and proximity. Deformation is spread across frames via deformationVerticesPerFrame. Meshes need Read/Write enabled.
Fields
collisionIgnoreTags
Collisions with the objects that have a tag that is on this list will be ignored. Collision state will be changed but no processing will happen.
Declaration
[Tooltip("Collisions with the objects that have a tag that is on this list will be ignored.\r\nCollision state will be changed but no processing will happen.")]
public List<string> collisionIgnoreTags
Field Value
| Type | Description |
|---|---|
| List<string> |
collisionTimeout
Disable repeating collision until the 'collisionTimeout' time has passed. Used to prevent a single collision triggering multiple times from minor bumps.
Declaration
[Tooltip("Disable repeating collision until the 'collisionTimeout' time has passed. Used to prevent single collision triggering multiple times from minor bumps.")]
public float collisionTimeout
Field Value
| Type | Description |
|---|---|
| float |
damage
Current vehicle (drivetrain) damage in range from 0 (no damage) to 1 (fully damaged).
Declaration
[NonSerialized]
public float damage
Field Value
| Type | Description |
|---|---|
| float |
damageIntensity
How much the new collisions add to the 'damage' value. Does not affect mesh deformation strength.
Declaration
[Tooltip("How much the new collisions add to the 'damage' value. Does not affect mesh deformation strength.")]
public float damageIntensity
Field Value
| Type | Description |
|---|---|
| float |
decelerationThreshold
Impact threshold needed to trigger damage. Compared against relative velocity scaled by 100.
Declaration
[Tooltip("Impact threshold needed to trigger damage (relative velocity x100).")]
public float decelerationThreshold
Field Value
| Type | Description |
|---|---|
| float |
deformationIgnoreTags
Objects that have a tag that is on this list will not have any meshes deformed on collision.
Declaration
[Tooltip("Objects that have a tag that is on this list will not have any meshes deformed on collision.")]
public List<string> deformationIgnoreTags
Field Value
| Type | Description |
|---|---|
| List<string> |
deformationRadius
Radius is which vertices will be deformed.
Declaration
[Range(0, 2)]
[Tooltip("Radius is which vertices will be deformed.")]
public float deformationRadius
Field Value
| Type | Description |
|---|---|
| float |
deformationStrength
Determines how much the vertices will be deformed for given collision strength.
Declaration
[Range(0.1, 5)]
[Tooltip("Determines how much the vertices will be deformed for given collision strength.")]
public float deformationStrength
Field Value
| Type | Description |
|---|---|
| float |
deformationVerticesPerFrame
Number of vertices that will be checked and eventually deformed per frame.
Declaration
[Tooltip("Number of vertices that will be checked and eventually deformed per frame. Setting it to lower values will reduce or remove frame drops but will induce lag into mesh deformation as vehicle will be deformed over longer time span.")]
public int deformationVerticesPerFrame
Field Value
| Type | Description |
|---|---|
| int |
falloffType
Falloff curve type for deformation. Linear = uniform falloff, Smoothstep = sharp edges with soft center, Exponential = very sharp edges.
Declaration
[Tooltip("Falloff curve type. Linear=uniform, Smoothstep=sharp edges soft center, Exponential=very sharp edges.")]
public FalloffType falloffType
Field Value
| Type | Description |
|---|---|
| FalloffType |
lastCollision
Collision data for the latest collision. Null if no collision yet happened. Only valid during the collision frame; deferred processing uses the snapshot fields instead.
Declaration
public Collision lastCollision
Field Value
| Type | Description |
|---|---|
| Collision |
lastCollisionTime
Time since startup to the latest collision.
Declaration
[Tooltip("Time since startup to the latest collision.")]
public float lastCollisionTime
Field Value
| Type | Description |
|---|---|
| float |
maxDeformationRatio
Maximum deformation distance as a ratio of the deformation radius to prevent extreme mesh distortion.
Declaration
[Range(0, 1)]
[Tooltip("Max deformation as ratio of radius to prevent extreme distortion. Lower = more conservative.")]
public float maxDeformationRatio
Field Value
| Type | Description |
|---|---|
| float |
meshDeform
Should meshes be deformed upon collision?
Declaration
[Tooltip("Should meshes be deformed upon collision?")]
public bool meshDeform
Field Value
| Type | Description |
|---|---|
| bool |
multiContactProcessing
When enabled, processes each contact point individually with distance weighting. Creates elongated/shaped deformation matching the impact area.
Declaration
[Tooltip("Process contacts individually for shape-aware deformation instead of averaging.")]
public bool multiContactProcessing
Field Value
| Type | Description |
|---|---|
| bool |
noiseScale
Perlin noise scale applied to vertex positions for sampling. Higher values produce finer detail.
Declaration
[Range(1, 50)]
[Tooltip("Perlin noise scale. Higher = finer detail in deformation variation.")]
public float noiseScale
Field Value
| Type | Description |
|---|---|
| float |
noiseStrength
Strength of Perlin noise contribution to deformation. 0 = no noise, 1 = full noise influence.
Declaration
[Range(0, 1)]
[Tooltip("Perlin noise strength. 0=none, 1=full influence.")]
public float noiseStrength
Field Value
| Type | Description |
|---|---|
| float |
surfaceAngleThreshold
Minimum facing toward the impact for a vertex to be deformed, measured as the dot of the vertex normal against the impact direction. -1 deforms any vertex in range, 0 only deforms vertices facing the impact; the back side of the mesh is left untouched.
Declaration
[Range(-1, 0)]
[Tooltip("Min facing toward impact for vertex deformation (-1=any vertex in range, -0.2=struck + side faces, 0=struck face only).")]
public float surfaceAngleThreshold
Field Value
| Type | Description |
|---|---|
| float |
surfaceConformance
Blend between impact velocity direction and contact surface normal (0 = use velocity only, 1 = use normal only). Higher values make deformation conform more to surface geometry.
Declaration
[Range(0, 1)]
[Tooltip("Blend between impact velocity and contact normal (0=velocity only, 1=normal only). Higher = more surface conformance.")]
public float surfaceConformance
Field Value
| Type | Description |
|---|---|
| float |
visualOnly
Should damage affect vehicle performance (steering, power, etc.)?
Declaration
[Tooltip("Should damage affect vehicle performance (steering, power, etc.)?")]
public bool visualOnly
Field Value
| Type | Description |
|---|---|
| bool |
Methods
AverageCollisionPoint(List<ContactPoint>)
Calculates average collision point from a list of contact points.
Declaration
public static Vector3 AverageCollisionPoint(List<ContactPoint> contacts)
Parameters
| Type | Name | Description |
|---|---|---|
| List<ContactPoint> | contacts |
Returns
| Type | Description |
|---|---|
| Vector3 |
Enqueue(Collision, float)
Add collision to the queue of collisions waiting to be processed.
Declaration
public bool Enqueue(Collision collision, float scaledImpactSpeed)
Parameters
| Type | Name | Description |
|---|---|---|
| Collision | collision | |
| float | scaledImpactSpeed |
Returns
| Type | Description |
|---|---|
| bool |
HandleCollision(Collision)
Processes a collision, queuing damage and deformation if it clears the timeout and threshold.
Declaration
public void HandleCollision(Collision collision)
Parameters
| Type | Name | Description |
|---|---|---|
| Collision | collision | Collision reported by the physics engine. |
MeshDeform(VehicleCollision, MeshFilter)
Deforms a single mesh's vertices for the given collision event.
Declaration
public void MeshDeform(DamageHandler.VehicleCollision collisionEvent, MeshFilter deformableMeshFilter)
Parameters
| Type | Name | Description |
|---|---|---|
| DamageHandler.VehicleCollision | collisionEvent | Queued collision driving the deformation. |
| MeshFilter | deformableMeshFilter | Mesh filter whose instance mesh is deformed. |
Repair()
Returns meshes to their original states.
Declaration
public void Repair()