Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Volume pvo = hit.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
- {
- ray = new Ray(hit.point, ray.direction); // now create a new ray starting at the hit position of the old ray
- var foundVoxel = false; // Keep track of whether we have previously found a voxel
- for (float d = 0; d < pvo.VoxelSize * (pvo.XSize >= pvo.YSize ? (pvo.ZSize >= pvo.XSize ? pvo.ZSize : pvo.XSize) : (pvo.ZSize >= pvo.YSize ? pvo.ZSize : pvo.YSize)) * 1.5f; d += pvo.VoxelSize * 0.5f) // iterate along the ray. We test for the largest XYZ dimension to make sure we pass all the way through the volume
- {
- Voxel? v = pvo.GetVoxelAtWorldPosition(ray.GetPoint(d)); // see if there's a voxel at the ray position
- if (v.HasValue)
- {
- //print("hI");
- foundVoxel = true;
- if (v.Value.Active)
- {
- // We have a voxel, and it's active, but we want to build a voxel so let's back up the ray a step
- Vector3 buildPos = ray.GetPoint(d - (pvo.VoxelSize * 0.5f));
- GameManager.instance.isTerrainUpdated = true;
- // Check that there is a voxel here, and it is inactive
- Voxel? v2 = pvo.GetVoxelAtWorldPosition(buildPos);
- if (v2.HasValue && !v2.Value.Active)
- {
- // Set this voxel to active, with a random color!
- pvo.SetVoxelAtWorldPosition(buildPos, new Voxel()
- {
- State = VoxelState.Active,
- Color = color,
- Value = 128
- });
- }
- break;
- }
- }
- else
- {
- if (!foundVoxel) continue; // We haven't found any voxels yet, so we can't have passed through the volume
- //print("hI ELSE");
- // We don't have a voxel, so we must have passed through the volume
- // Back the ray up a step
- Vector3 buildPos = ray.GetPoint(d - (pvo.VoxelSize * 0.5f));
- // Check that there is a voxel here, and it is inactive
- Voxel? v2 = pvo.GetVoxelAtWorldPosition(buildPos);
- if (v2.HasValue && !v2.Value.Active)
- {
- // Set this voxel to active, with a random color!
- pvo.SetVoxelAtWorldPosition(buildPos, new Voxel()
- {
- State = VoxelState.Active,
- Color = color,
- Value = 128
- });
- }
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment