Skip to content

Instantly share code, notes, and snippets.

@d-lowl
Created July 10, 2018 16:56
Show Gist options
  • Save d-lowl/85cd5e2b0887361314925084a4db557b to your computer and use it in GitHub Desktop.
Save d-lowl/85cd5e2b0887361314925084a4db557b to your computer and use it in GitHub Desktop.
Explosion script for MagicaVoxel models in Unity
1. A model must be imported with https://github.com/darkfall/MagicaVoxelUnity
2. Attach the script to an object with MVVoxModel component.
3. Set magnitude and call Explode method
WARNING: Due to the nature of re-importing in realtime with individual voxels for explosions, large models may be laggy or can even freeze the app.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VoxExplosion : MonoBehaviour {
public float forceMagnitude = 1000;
public void Explode() {
MVVoxModel model = this.gameObject.GetComponent<MVVoxModel> ();
model.ed_importAsIndividualVoxels = true;
model.LoadVOXFile (model.ed_filePath, model.ed_importAsIndividualVoxels);
foreach (Transform v in this.transform) {
v.gameObject.AddComponent<BoxCollider>();
var r = v.gameObject.AddComponent<Rigidbody>();
var forceDirection = Random.onUnitSphere;
r.AddForce(forceDirection*forceMagnitude);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment