private void FixedUpdate() { if (!gameObject.activeSelf || DetectionPoints.Count == 0 || Collided == null) { return; } foreach (GameObject o in GameObject.FindGameObjectsWithTag("PicaVoxelVolume")) { if (o == gameObject || !o.activeSelf) { continue; } Volume pvo = o.GetComponent <Volume>(); foreach (Vector3 pos in DetectionPoints) { Voxel?pv = pvo.GetVoxelAtWorldPosition(transform.position + pos); if (pv.HasValue && pv.Value.Active) { Collided(pvo, pv.Value, transform.position + pos); break; } } } }
// Use DetectCollision to detect hits manually public bool DetectCollision(Vector3 worldPos, out Voxel voxel, out Volume hitObject) { foreach (GameObject o in GameObject.FindGameObjectsWithTag("PicaVoxelObject")) { Volume pvo = o.GetComponent <Volume>(); Voxel? pv = pvo.GetVoxelAtWorldPosition(worldPos); if (pv.HasValue && pv.Value.Active) { hitObject = pvo; voxel = pv.Value; return(true); } } hitObject = null; voxel = new Voxel(); return(false); }
public void Explode(Vector3 additionalVelocity) { foreach (GameObject o in GameObject.FindGameObjectsWithTag("PicaVoxelVolume")) { if (ExplodeTarget == ExplodeTargets.AllButSelf && o.transform.root == transform.root) { continue; } if (ExplodeTarget == ExplodeTargets.SelfOnly && o.transform.root != transform.root) { continue; } Volume pvo = o.GetComponent <Volume>(); if (pvo == null) { continue; } Vector3 cpob = pvo.Hitbox.ClosestPointOnBounds(transform.position); if (Vector3.Distance(transform.position, cpob) <= ExplosionRadius || pvo.GetVoxelAtWorldPosition(transform.position) != null) { Batch batch = pvo.Explode(transform.position, ExplosionRadius, ValueFilter, ValueFilterOperation); if (batch.Voxels.Count > 0 && VoxelParticleSystem.Instance != null) { VoxelParticleSystem.Instance.SpawnBatch(batch, pos => (((pos + Random.insideUnitSphere) - transform.position) * ((Random.Range(ParticleVelocity - 1f, ParticleVelocity + 1f)))) + additionalVelocity); } batch.Dispose(); } } }
// Update is called once per frame private void Update() { int numParts = System.GetParticles(parts); if (numParts > 0 && (CollideNegativeX || CollideNegativeY || CollideNegativeZ || CollidePositiveX || CollidePositiveY || CollidePositiveZ)) { foreach (GameObject o in GameObject.FindGameObjectsWithTag(CollisionTag)) { Volume pvo = o.GetComponent <Volume>(); for (int p = 0; p < numParts; p++) { if (CollideNegativeX && parts[p].velocity.x < 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(-parts[p].GetCurrentSize(System), 0, 0)); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(-(parts[p].velocity.x * BounceMultiplier), parts[p].velocity.y, parts[p].velocity.z); } } if (CollidePositiveX && parts[p].velocity.x > 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(parts[p].GetCurrentSize(System), 0, 0)); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(-(parts[p].velocity.x * BounceMultiplier), parts[p].velocity.y, parts[p].velocity.z); } } if (CollideNegativeY && parts[p].velocity.y < 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(0, -parts[p].GetCurrentSize(System), 0)); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(parts[p].velocity.x, -(parts[p].velocity.y * BounceMultiplier), parts[p].velocity.z); } } if (CollidePositiveY && parts[p].velocity.y > 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(0, parts[p].GetCurrentSize(System), 0)); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(parts[p].velocity.x, -(parts[p].velocity.y * BounceMultiplier), parts[p].velocity.z); } } if (CollideNegativeZ && parts[p].velocity.z < 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(0, 0, -parts[p].GetCurrentSize(System))); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(parts[p].velocity.x, parts[p].velocity.y, -(parts[p].velocity.z * BounceMultiplier)); } } if (CollidePositiveZ && parts[p].velocity.z > 0) { Voxel?v = pvo.GetVoxelAtWorldPosition(parts[p].position + new Vector3(0, 0, parts[p].GetCurrentSize(System))); if (v.HasValue && v.Value.Active) { parts[p].velocity = new Vector3(parts[p].velocity.x, parts[p].velocity.y, -(parts[p].velocity.z * BounceMultiplier)); } } } } System.SetParticles(parts, numParts); } }