Skip to content

Instantly share code, notes, and snippets.

@GarethIW
Created May 11, 2019 10:12
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/41087d8c211373d1e5215eb69cea481e to your computer and use it in GitHub Desktop.
Save GarethIW/41087d8c211373d1e5215eb69cea481e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using PicaVoxel;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float Speed;
public Vector3 Direction;
public float ExplodeRadius;
public float ParticleVelocity;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position += Direction * (Speed * Time.deltaTime);
}
private void OnCollisionEnter(Collision other)
{
var vol = other.gameObject.GetComponent<Volume>();
// Bullet hit something so kill it even if it is not a PicaVoxel volume
Destroy(gameObject);
if (vol == null) return; // add other target object checks here - is an enemy? etc
var explodePos = other.GetContact(0).point;
// We have the point at which the bullet hits the collision mesh, now lets step through in the direction of travel until we hit a voxel
for (var d = 0f; d < 100f; d += vol.VoxelSize)
{
var checkPoint = explodePos + (Direction * d);
var vox = vol.GetVoxelAtWorldPosition(checkPoint);
if (vox.HasValue && vox.Value.State==VoxelState.Active)
{
var particleBatch = vol.Explode(checkPoint, ExplodeRadius, 0, Exploder.ExplodeValueFilterOperation.GreaterThanOrEqualTo);
// volume explosion returns a batch of particles which we then pass to the VoxelParticleSystem
VoxelParticleSystem.Instance.SpawnBatch(particleBatch, pos => ((pos + Random.insideUnitSphere) - pos)*
(Random.Range(ParticleVelocity - 1f, ParticleVelocity + 1f)));
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment