Fracturing & Destruction Scripting

Here we describe the exposed methods you may use through scripting.

Besides the collision notifications, there are 2 main classes you will be accessing.

  • FracturedObject: The GameObject that has the FracturedObject attached.
  • FracturedChunk: Each chunk that the FracturedObject creates when the fracturing is computed. Each chunk GameObject will have one.

FracturedObject methods:

void Explode(Vector3 v3ExplosionPosition, float fExplosionForce)

Explodes the whole object into chunks.

  • v3ExplosionPosition: The source point of the explosion.
  • fExplosionForce: The explosion intensity.

void Explode(Vector3 v3ExplosionPosition, float fExplosionForce, float fRadius, bool bPlayExplosionSound, bool bInstanceExplosionPrefabs, bool bAlsoExplodeFree, bool bCheckStructureIntegrityAfter)

Explodes the object partially, because an influence radius is given.

  • v3ExplosionPosition: The explosion source position.
  • fExplosionForce: The explosion intensity.
  • fRadius: The explosion radius. Only chunks inside this radius will be affected by the explosion.
  • bPlayExplosionSound: Will the explosion sound clip be played? This sound clip is configured through the component panel, on the events section.
  • bInstanceExplosionPrefabs: Will the explosion instance prefabs? Prefabs will most likely be explosion particles, and are configured also through the component panel on the events section.
  • bAlsoExplodeFree: This tells if the free chunks (those that already have been detached from the object) will be affected by the explosion as well.
  • bCheckStructureIntegrityAfter: Set this to true if the object has connection information (through the "Connection Information" parameter) and needs to be checked for possible collapsing chunks afterwards.

void CollapseChunks()

Collapses the whole object setting all chunks free.

void ResetChunks()

Resets all chunks to their initial, unbroken, position. The only condition is no chunk was deleted before the call.

FracturedChunk methods:

static FracturedChunk ChunkRaycast(Vector3 v3Pos, Vector3 v3Forward, out RaycastHit hitInfo)

Returns the chunk that intersects a raycast from position v3Pos looking to direction v3Forward, or null if there is none.
hitInfo will contain information about the raycast (see Unity's reference of RayCastHit).

void Impact(Vector3 v3Position, float fExplosionForce, float fRadius, bool bAlsoImpactFreeChunks)

Use this to notify an impact (bullet, missile, any object in general) on a chunk that is still attached to the structure. Usually what you would do is raycast from the weapon to see if any chunk is in sight, and if the weapon fires then call this method if it hits. This will generate an explosion with the following parameters:

  • v3Position: The impact position (given by the raycast).
  • fExplosionForce: The explosion intensity.
  • fRadius: The explosion radius. All chunks inside this radius will be detached and exploded too.
  • bAlsoImpactFreeChunks: Will also explode free chunks if they are inside the radius.

void DetachFromObject(bool bCheckStructureIntegrity = true)

Detaches a chunk from the structure. If bCheckStructureIntegrity is true it performs a structure integrity check to see if any more chunks need to be collapsed (use this only if the "Connection Information" parameter in the inspector is set).

Collision notifications:

If "Call Method Name" and "Call GameObject" parameters were set on the corresponding Events sections on the inspector panel, you will be notified when chunks were detached or did collide. This will allow to do custom processing in case the prefab instancing or the audio clips aren't enough.
The method that will be called will receive a FracturedChunk.CollisionInfo object. Like this, for example:

void OnChunkNotification(FracturedChunk.CollisionInfo info)
// Do something fancy here

Important! Unlike the other 2, the event in section "On Every Chunk Detach Event" will not receive any parameters. This event will be triggered every time a chunk is detached from the object, no matter the reason. The event "Chunk Detach From Öbject Due To Physics Collision" will only be triggered if a rigidbody collided with the object, but not if for instance a script call (FracturedObject.Explode(), FracturedObject.CollapseChunks(), FracturedChunk.Impact(), FracturedChunk.DetachFromObject()...) triggered it.

FracturedChunk.CollisionInfo has the following parameters:

  • chunk (type FracturedChunk): The chunk that received the collision and is going to detach or collide (depending on the notification requested).
  • collisionInfo (type Collision): The collision information that Unity 3D provides. Use this to get the contact points or normals for example.
  • bIsMain (bool): Only if you requested the detach notifications. For non detached chunks, tells if it is the main chunk (the one that was hit) or if it is a neighbour that went also with it (due to FracturedObject.ChunkConnectionStrength being < 1).
  • bCancelCollisionEvent (bool): If set to true, it will cancel the collision event so that it won't do anything.