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 Carbongrip · Feb 22, 2014 at 05:42 AM · c#guicrosshairsnappingblocks

Snapping Issue

Ok so I have this script and have a snapping issue that makes no sense. I create a chunk with a for statement and then I create a few structures with for statements also. When I place a block it usually work but sometimes it decides to place the block inside another(same location). How can I eliminate this issue? I think its a rounding issue! Also while I have attention is there a way to not have the mouse and center this action where you create the gui crosshair? How can I make the gui crosshair? Just use the games gui function?

 void Update()
     {
         Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
 
         Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
 
         if(Physics.Raycast(ray, out hit))
         {
             if(Input.GetMouseButtonDown(0))
             {
                 Destroy(hit.collider.gameObject);
             }
 
             if(Input.GetMouseButtonDown(1))
             {
                 Instantiate(prefab, G, Quaternion.identity);
             }
         }
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Carbongrip · Mar 30, 2014 at 04:09 AM

Well I finally figured it out after taking a break from the project... I was so close with original code. This is what worked out for me...

 void Update()
     {
         Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
 
         if(Physics.Raycast(ray, out hit))
         {
             if(Input.GetMouseButtonDown(0))
             {
                 Destroy(hit.collider.gameObject);
             }
 
             if(Input.GetMouseButtonDown(1))
             {
                 if(prefab != null)
                     Instantiate(prefab, hit.transform.position + hit.normal, Quaternion.identity);
                 else
                     return;
             }
         }
Comment
Add comment · Share
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
0

Answer by RudyTheDev · Feb 22, 2014 at 12:48 PM

The best I understand is that you have a voxel-based world type game. You pick a position a short bit in front of the camera (round it off) and place a block there. But you aren't doing anything in the code above to check if there is already a block there. So nothing is stopping the game from creating the block at the same place as other blocks.

The fix is "simple" -- you don't create a block if there is one already there. (Usually, voxel-based games place blocks next to the face of the block you are looking at, but it seems you simply place it in front of the camera.) So, in your case, you probably want Physics.OverlapSphere() or similar to detect if there is something already there at G. In the simplest case:

 if (Physics.OverlapSphere(G, 0.1f).Length == 0) // 0 colliders are within 0.1f around location G
     // Your block placing code
Comment
Add comment · Show 12 · Share
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 Carbongrip · Feb 22, 2014 at 06:41 PM 0
Share

Well that prevents objects from being placed where they shouldn't but then they hardly ever place. I do however don't have a voxel world, I found a better solution to the fps problem. This is because I tried voxels and didn't understand it enough. How can I make this work with simple cubes?

avatar image RudyTheDev · Feb 22, 2014 at 07:33 PM 0
Share

I don't quite understand your reply. Anyway, I didn't say you have voxel-based world, I said voxel-based world type game. It is usually done with voxels, but you can do it with whatever fits, like GameObject cubes, if that works. $$anonymous$$y reply didn't assume voxels, in fact, it wouldn't even work with a true voxel mesh.

So what exactly does not work? Objects not placed where they shouldn't sounds like it "works". Perhaps you haven't explained what it is that you want to begin with?

avatar image Carbongrip · Feb 22, 2014 at 07:48 PM 0
Share

I just have a issue placing objects now. Like now it won't place it in a bad spot but you can't get it to place at that point ever.

Also noticed it always places the block on other blocks spawned with this function just picky with the generated objects from for loops.

Edit: Just noticed its happening when I try placing on sides of blocks. So when I try to place on sides sometimes it does nothing.

avatar image getyour411 · Feb 22, 2014 at 08:01 PM 0
Share

re: the crosshair

Create a small graphic (24x24ish) and place it at screen.width/2, screen.height/2; to turn off normal cursor there's a standard function for that- screen.cursor = off/false or something like that do a search

avatar image RudyTheDev · Feb 23, 2014 at 12:18 PM 0
Share

@Carbongrip: "it won't place it in a bad spot but you can't get it to place at that point ever" -- make sure there are no collisions there. Iterate through the Physics.OverlapSphere(G, 0.1f) array and see what it actually collides with at that point (and what that point really is).

"it always places the block on other blocks spawned with this function" -- that's may be because you haven't attached a collider to them, so the no-collision check passes.

"try placing on sides of blocks" -- you need to post your "place on sides of blocks" code. The code in the question doesn't include any logic for this.

P.S. It would also help you with debugging if you made a dummy block that constantly repositions itself to where the G is.

Show more comments

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

How to make the Crosshair follow delayed 1 Answer

Multiple Cars not working 1 Answer

Drawing 4 GUIButton's with a forloop. Need help changing color? 1 Answer

GUI objects jitter when following "lerped" Gameobject [SOLVED] 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