Author Topic: Getting the voxel just above the terrain  (Read 403 times)

zarkrat

  • Newbie
  • *
  • Posts: 6
    • View Profile
Getting the voxel just above the terrain
« on: February 01, 2019, 09:36:56 PM »
Hello.

I need to place a voxel on top of the terrain. There is the method "GetTerrainHeight", but this method returns information of the heightmap only. My terrain can be destroyed dynamically, so I need the position of the lowest voxel at real time. To do this, I tried to use a reverse raycast by starting below terrain going upwards at the exact x,z position that I need, in the hope that the ray would only hit the "surface". However, this is not what happens. Invisible voxels below surface get hit instead and the hit never reaches the upper plane.

I am using the following method:

Code
            VoxelHitInfo hitInfo;
            if(RayCastFast(start_position, end_position, out hitInfo, max_size, false, 15, ColliderTypes.OnlyVoxels))
            {
                Debug.DrawLine(start_position, end_position, Color.yellow, 50);

                if (!IsEmptyAtPosition(hitInfo.voxelCenter))
                {
                    VoxelDestroy(hitInfo.voxelCenter);
                }

            }

This is the result that I got:



A voxel below surface was destroyed.

Thanks for help.
« Last Edit: February 01, 2019, 09:56:47 PM by zarkrat »

zarkrat

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Getting the voxel just above the terrain
« Reply #1 on: February 02, 2019, 01:04:43 AM »
I managed to do it with two rays. First I use a Physics.Raycast upwards, which ignores the under surface voxels and hits the first above surface it finds. Then I get this voxel above surface and simply call "GetVoxelUnderIndex" on it.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4843
    • View Profile
Re: Getting the voxel just above the terrain
« Reply #2 on: February 02, 2019, 04:50:31 PM »
You can also use GetVoxelUnder method. It basically casts a RayCastFast downwards from the position given.