MD Package Technical & APi Documentation
MDM_MeshDamage :: MD_ModifierBase
Namespace: MDPackage.Modifiers
Damage/ distort physical mesh by specific parameters.
Read more here (Online documentation slide)
Public Methods
// Modify current mesh by the point (world space), radius, force and direction
public void MeshDamage_ModifyMesh(Vector3 point, float radius, float force, Vector3 direction, bool continuousEffect = false)
// Refresh vertices & register brand new original vertices state
public void MeshDamage_RefreshVertices()
// Restore deformed mesh by the specified speed value
public void MeshDamage_RestoreMesh(float restorationSpeed = 0.1f)
// Modify current mesh by the custom RaycastEvent
public void MeshDamage_ModifyMesh(MDM_RaycastEvent RayEvent)
Public Fields
public bool detectForceImpact = true; // If enabled, the force will be automatically calculated from the rigidbody's velocity
public float forceAmount = 0.15f; // If the above field is disabled, this will be used as constant
public float forceMultiplier = 0.075f; // General force multiplier
public bool detectRadiusSize = false; // If enabled, the impact radius will be automatically calculated from the rigidbody's scale
public float radius = 0.5f; // If the above field is disabled, this will be used as constant
public float radiusMultiplier = 1.0f; // General radius multiplier
public float radiusSoftness = 1.0f; // Edge smoothness of the radius
public float forceDetection = 1.5f; // Minimum rigidbody velocity to detect

public bool continousDamage = false; // Can the mesh go further than its original vertices after deformation?

public bool collisionWithSpecificTag = false;
public string collisionTag = "";

public bool enableEvent;
public UnityEvent eventOnDamage;
Examples
The following paragraphs contain Mesh Damage modifier component used in a practical example - damage specific mesh with raycast at runtime.
using UnityEngine;
using MDPackage.Modifiers;

[RequireComponent(typeof(MeshFilter))]
public class SampleScript : MonoBehaviour
{
// Cache main camera in the inspector!
public Camera mainCam;
 [Space]
// Edit damage parameters
public float damageRadius = 0.25f;
public float damageForce = 0.5f;
public bool continuousEffect = true;

private MDM_MeshDamage damageable;

private void Start()
 {
  // Add/Create a Mesh Damage modifier to this object right after start
  damageable = MD_ModifierBase.CreateModifier<MDM_MeshDamage>(gameObject, MD_ModifierBase.MeshReferenceType.CreateNewReference);
  // Add MeshCollider for raycast detection
  damageable.gameObject.AddComponent<MDPackage.MD_MeshColliderRefresher>();
 }

private void Update()
 {
  // Main camera must be initialized
  if (!mainCam)
   return;

  // Sample extension - if R is held, restore mesh to its original state by the specific restoration speed
  if (Input.GetKey(KeyCode.R))
   damageable.MeshDamage_RestoreMesh(0.01f);

  if (Input.GetMouseButtonDown(0))
  {
   // Create raycast from camera to the cursor position
   Ray r = mainCam.ScreenPointToRay(Input.mousePosition);
   bool hit = Physics.Raycast(r, out RaycastHit h) && h.transform == damageable.transform;
   // Checking for potential hit - if the hit collider equals the damageable, damage the mesh
   if (hit) // Modify damageable mesh by the hit point, damage radius, damage force, direction and continuous effect
    damageable.MeshDamage_ModifyMesh(h.point, Mathf.Abs(damageRadius), -Mathf.Abs(damageForce), h.normal, continuousEffect);
  }
 }
}