Created
July 10, 2018 16:56
-
-
Save d-lowl/85cd5e2b0887361314925084a4db557b to your computer and use it in GitHub Desktop.
Explosion script for MagicaVoxel models in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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