Skip to content

Instantly share code, notes, and snippets.

@GarethIW
Created January 16, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarethIW/207bf07e76fa77fca020 to your computer and use it in GitHub Desktop.
Save GarethIW/207bf07e76fa77fca020 to your computer and use it in GitHub Desktop.
Ray r = new Ray(GetComponent<Camera>().transform.position,GetComponent<Camera>().transform.forward);
Debug.DrawRay(r.origin, r.direction*100f, Color.red, 10f);
RaycastHit hitInfo;
if (Physics.Raycast(r, out hitInfo)) // first, cast the ray using normal Unity physics. Don't forget to include a layer mask if needed
{
Volume pvo = hitInfo.collider.GetComponentInParent<Volume>();
if (pvo != null) // check to see if we have hit a PicaVoxel Volume. because the Hitbox is a child object on the Volume, we use GetComponentInParent
{
r = new Ray(hitInfo.point, r.direction); // now create a new ray starting at the hit position of the old ray
for (float d = 0; d < 50f; d += pvo.VoxelSize*0.5f) // iterate along the ray. we're using a maximum distance of 50 units here, you should adjust this to a sensible value for your scene
{
Voxel? v = pvo.GetVoxelAtWorldPosition(r.GetPoint(d)); // see if there's a voxel at the ray position
if (v.HasValue && v.Value.Active)
{
// We have a voxel (v), a collison position (r.GetPoint(d)), and a Volume (pvo)
// so we can now do anything we like
Batch b = pvo.Explode(r.GetPoint(d), 1f, 0, Exploder.ExplodeValueFilterOperation.GreaterThanOrEqualTo);
VoxelParticleSystem.Instance.SpawnBatch(b, pos =>
((pos + Random.insideUnitSphere) - transform.position) *
Random.Range(3f - 1f, 3f + 1f));
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment