Raycast-ing to get the hit Volume on scene full of runtime-positioned Volumes

0
abunner posted this 01 March 2016

First: I love this plugin. I'm a Unity newbie and was able to very quickly get things rendering and moving around. Thanks!

I have scene full of instantiated prefabs that I position and resize at runtime. Each Volume is basically a tile on a map with ~64 of them in a scene. It's probably not the fastest way to do what I'm doing, but it's working.

I'm trying to translate a click/touch into the Volume or Volumes that were touched. At first, I found that each ray would hit several Volumes--I assumed that was because the Hitboxes for each Volume were much bigger than the Voxels themselves. So I tried resizing the Hitbox on the prefab, but now my ray hits nothing.

I think it's because the colliders on my Volumes aren't aligned with where the Voxels actually are. But I don't know how to fix it.

 // Figure out the translation
 Vector3 position = /* translate a tile position to Unity-space */

 // Instantiate the PicaVoxel GameObject
 GameObject gameObject = (GameObject) Instantiate (emptyTerrain, position, Quaternion.identity);

 // sim is an instance of my own class that represents a tile
 // sim.dimensions is the voxel-dimensions of a tile

 // Resize the volume to be the right number of voxels
 Volume volume = gameObject.GetComponent<volume> ();
 volume.VoxelSize = 0.25f;
 volume.XSize = sim.dimensions.x;
 volume.YSize = sim.dimensions.y;
 volume.ZSize = sim.dimensions.z;
 volume.GetCurrentFrame().Voxels = new Voxel[sim.dimensions.x * sim.dimensions.y * sim.dimensions.z];
 volume.GetCurrentFrame ().XSize = sim.dimensions.x;
 volume.GetCurrentFrame ().YSize = sim.dimensions.y;
 volume.GetCurrentFrame ().ZSize = sim.dimensions.z;

 // Set the voxels
 for (int i = 0; i < sim.cubes.length; i++) {
      // fill in voxels from the sim object into volume's current frame
 }

 // call updateallchunks
 volume.frames[0].updateallchunks();

and then i try to raycast to figure out what thing i touched/clicked:

ray ray = camera.main.screenpointtoray (input.mouseposition);
// good news, it draws a ray that touches the right thing
debug.drawray (ray.origin, ray.direction * 100f, color.red, 10f);
raycasthit hit;
physics.raycast (ray, out hit);
// uh-oh hit.collider is null

in the scene-view when the game is running i can see the that hitbox draws way off in a different part of the screen than the voxels are drawing so i suspect that's it. any help on how to correct? fundamentally, i don't understand how things like the object's scale, the voxel size (i'm using 0.25), the hitbox and the volume's transform all figure into the collision detection.

thanks again for the great plugin! sim.cubes.length;="" i++)="" {="" fill="" in="" voxels="" from="" the="" sim="" object="" into="" volume's="" current="" frame="" }="" call="" updateallchunks="" volume.frames[0].updateallchunks();="" and="" then="" i="" try="" to="" raycast="" to="" figure="" out="" what="" thing="" i="" touched/clicked:="" ray="" ray="camera.main.screenpointtoray" (input.mouseposition);="" good="" news,="" it="" draws="" a="" ray="" that="" touches="" the="" right="" thing="" debug.drawray="" (ray.origin,="" ray.direction="" ="" 100f,="" color.red,="" 10f);="" raycasthit="" hit;="" physics.raycast="" (ray,="" out="" hit);="" uh-oh="" hit.collider="" is="" null="" in="" the="" scene-view="" when="" the="" game="" is="" running="" i="" can="" see="" the="" that="" hitbox="" draws="" way="" off="" in="" a="" different="" part="" of="" the="" screen="" than="" the="" voxels="" are="" drawing="" so="" i="" *suspect="" that's="" it.="" any="" help="" on="" how="" to="" correct?="" fundamentally,="" i="" don't="" understand="" how="" things="" like="" the="" object's="" scale,="" the="" voxel="" size="" (i'm="" using="" 0.25),="" the="" hitbox="" and="" the="" volume's="" transform="" all="" figure="" into="" the="" collision="" detection.="" thanks="" again="" for="" the="" great="">

 // call updateallchunks
 volume.frames[0].updateallchunks();

and then i try to raycast to figure out what thing i touched/clicked:

ray ray = camera.main.screenpointtoray (input.mouseposition);
// good news, it draws a ray that touches the right thing
debug.drawray (ray.origin, ray.direction * 100f, color.red, 10f);
raycasthit hit;
physics.raycast (ray, out hit);
// uh-oh hit.collider is null

in the scene-view when the game is running i can see the that hitbox draws way off in a different part of the screen than the voxels are drawing so i suspect that's it. any help on how to correct? fundamentally, i don't understand how things like the object's scale, the voxel size (i'm using 0.25), the hitbox and the volume's transform all figure into the collision detection.

thanks again for the great plugin!>

Last edited 02 March 2016

2 Comments
Order By: Standard | Newest | Votes
1
GarethIW posted this 01 March 2016

After resizing a volume, you probably want to call CreateChunks() instead of UpdateAllChunks(). This will ensure all the chunks are the right size and it will also update the Hitbox collider.

I'm not sure that this would fix the hitbox appearing way off from the parent volume though, so it sounds like you're doing something funky with local vs world transforms as well.

If the above doesn't help, I'll probably need to see the project or at least some screenshots or something to help further.

0
abunner posted this 02 March 2016

That was it! CreateChunks() did the trick

The Hitbox is in the right place. Working great. Thanks!


Our 125 members have posted 459 times in 111 discussions