Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JasonBricco · Jun 06, 2014 at 12:39 AM · raycastcollidersraycastingvoxel

Voxel Raycasting Help

Hi all.

I have a voxel world, made up of cubes (you know, like Minecraft - except my game isn't like Minecraft. But it's voxel). And before, I was using mesh colliders for each chunk of 16x64x16 blocks. As you can imagine, mesh colliders are not ideal and were causing some hiccuping upon chunk loading. So I dumped the mesh colliders and decided to use box colliders that checked blocks around the player, and, if they were solid blocks that can be collided with, they would be enabled there.

This performs well. But it comes with a problem. Before, to place and delete blocks, I would cast a ray at the ground, get the hit point, use the normal of it to figure out where to place the block or to figure out which block to delete.

Well, now I don't have mesh colliders anymore and so there aren't colliders to hit with rays. I posted another question earlier about collider performance, but I don't think it's feasible to go with the collider approach. I think I need to use a custom raycast.

I wrote one that's really simple. Here it is (with a few details omitted for simplicity):

 public static bool Raycast(Ray ray, int distance, bool adding, out VoxelCoords coords)
 {
     float dist = 0.0f;
     Vector3 point;
     
     for (int i = 0; i < distance; i++)
     {
         dist += 1.0f;
         point = ray.GetPoint(dist);
         VoxelCoords tempCoords = new VoxelCoords(point);
 
         if (ReadBlock(tempCoords).type != Blocks.air)
         {
             coords = adding ? new VoxelCoords(ray.GetPoint(dist - 1.0f)) : tempCoords;
             return true;
         }
     }
     
     coords = new VoxelCoords(false);
     return false;
 }

You give it a ray, a distance, whether you're adding a block or removing a block, and some Voxel coordinates (which are just Vector3s but rounded to a block's center, for reading voxel data).

The ray I pass in is a ray obtained from using ScreenPointToRay from Input.mousePosition.

I figured I would just advance along the ray 1 unit at a time and use GetPoint to figure out the point there. Then I would round it to voxel coordinates, figure out if air is there. If there isn't air, then we hit something. Then if adding is true, I return 1 unit back on the ray, otherwise I return the coordinates hit.

So. This fails. It generally works, but it's not accurate. The blocks modified aren't quite where I click, they may be off and affect another block to the side of the one I'm trying to modify.

One issue I can see with it is that when I use the ray.GetPoint(dist - 1.0f), it's not acting like a normal that's perpendicular to the block face. I can probably use the ray to figure out the direction properly to fix that.

The part I don't understand is why, even when I'm deleting the blocks (adding = false), it's still not accurate. It just returns the coordinates of the block that was hit. But it seems to be hitting the wrong one at times.

So I think it's not as simple as I'm trying to make it here. My question, then, is I'm wondering if you guys have any advice or pointers to help me get this working better.

Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JasonBricco · Jun 12, 2014 at 03:47 AM 0
Share

I've decided to try a different method, so this is no longer relevant.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit returns object without collider, in wrong layer 1 Answer

Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer

How can I let the ray to the edge of the capsule? 1 Answer

Raycast not hitting the collider properly it always has a weird offset 0 Answers

Raycasting to make Buildings Clickable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges