Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by SoulRider · Aug 20, 2015 at 12:22 PM · gridsnapplanesrounding

Rounding Errors when snapping to grid

I am making a building game which has freeform and snap to grid build options. When using freeform, the below code works perfectly, but when useGridSnap is true, the rounding used in this example causes problems:

     void PositionGhost(){
         Vector3 targetPos = rayHitPoint;
         if (useGridSnap) {
             /*Bug: This rounding causes an error which sometimes makes the block 
              * position itself incorrectly when placing on grid */
             ghostTrans.position = new Vector3 (Mathf.Round (targetPos.x),
                                               Mathf.Round (targetPos.y),
                                               Mathf.Round (targetPos.z));
         } else {
             ghostTrans.position = rayHitPoint;
         }
         ghostTrans.up-=(ghostTrans.up-rayHitNormal);
         ghostTrans.Translate (new Vector3(0,buildOffset,0));
         CheckBuildValid ();
     }

Specifically, the snap to grid works perfectly when placing on top of an object, but is thrown off when trying to place on the side of an object. Freeform offsets correctly whether on top or side of object.

I need a way to improve the accuracy of the rounding, maybe by taking into account the plane normal that is being hit and only returning the rounding of the X & Z relative to the plane? Although I can see that still giving me issues.

Any advice would be appreciated.

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 SoulRider · Aug 20, 2015 at 12:28 PM 1
Share

Here is a quick video showing the issue - https://youtu.be/c3aTqy4Ii2c

2 Replies

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

Answer by SoulRider · Aug 21, 2015 at 06:24 PM

Thanks for the help with this issue, I have now come up with a working solution, which I'll post back as I always get infuriated when someone fixes something themselves and doesn't post how.

Thanks for pointing me in the object position direction @Adam Halley-Prinable

I simply hit an object, get it's plane normal and it's position, then shift the ghost object by 1 grid position in it's new 'up' direction. If I hit the the terrain I just use the grid as normal.

This should work with any object, whether origin in middle, or at bottom, however, it will cause issues if trying to combine the two.

Here is the new working function for this issue:

 void PositionGhost(){
             if (useGridSnap) {
                 if (hitObject) {
                     ghostTrans.position = hitObject.transform.position;
                     ghostTrans.up-=(ghostTrans.up-rayHitNormal);
                     ghostTrans.Translate (new Vector3(0,1,0));
                 }
                 else {
                     ghostTrans.position = new Vector3 (Mathf.Round rayHitPoint.x),
                                                        Mathf.Round (rayHitPoint.y),
                                                        Mathf.Round (rayHitPoint.z));
                     ghostTrans.up-=(ghostTrans.up-rayHitNormal);
                     ghostTrans.Translate (new Vector3(0,buildOffset,0));
                 }
             } else {
                 ghostTrans.position = rayHitPoint;
                 ghostTrans.up-=(ghostTrans.up-rayHitNormal);
                 ghostTrans.Translate (new Vector3(0,buildOffset,0));
             }
             CheckBuildValid ();
         }
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 Adam-Halley-Prinable · Aug 20, 2015 at 01:49 PM

I would make my own rounding function and custom-make it for this purpose. Round every number in a vector3 to the nearest multiple of a grid space width

Comment
Add comment · Show 3 · 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 SoulRider · Aug 20, 2015 at 04:59 PM 0
Share

It does that already. The grid space width is 1, default unity. Round automatically makes everything an int, so a multiple of 1.

avatar image Adam-Halley-Prinable · Aug 20, 2015 at 05:16 PM 1
Share

Could it be that it's snapping to world space and not snapping to the position of that box +/- 1?

also, shouldn't targetPos be the position of the object raycasted to, rather than the position where the raycast landed?

avatar image SoulRider · Aug 20, 2015 at 07:56 PM 0
Share

The target position needs to be where I want to place the object, so this is the point on the collider where my raycast hits. I use ClosestPointOnBounds for this. This works for the freeform mode, but you are right, it might be causing an issue with the Snap $$anonymous$$ode. When I adjust for the origin in the centre of the objects. This is where the issue seems to be occurring.

I doubled the buildOffset, and it solves the issue of offset placement using snap to grid. However, I am left with my blocks floating above the ground ins$$anonymous$$d of placing on it. It's a semi-solution, well, once I resolve the floating above ground issues.

I played with using the object I think the real problem is the origin being in the centre of the block. If it was at the bottom, I believe I wouldn't have this issue..

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is there a toggle for snap to grid? 10 Answers

Gizmo won't stop snapping to grid. 1 Answer

Snap object to grid 1 Answer

Check if object is on grid 1 Answer

How to drag gameobject (only x y) snapped to a grid? 2 Answers


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