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 Zerebokep · May 22, 2015 at 03:25 PM · rotationgridsnapping

Increment grid by rotation

I'm currently working on a grid for snapping objects side by side. My current script is working fine, but only for game objects which haven't been rotated.

Here is an example video of the problem:

https://vid.me/jN6D

The cube at the right has a rotation of Vector3.zero, the left one has been rotated on the Y axis.

As you can see snapping works perfectly for the right one, but at 0:15 you can see that the snapping doesn't work well for the rotated cube, that's because I've currently no idea how I can add the rotation in my current script.

Here is a snippet of the most important part of code I'm currently using to calculate the grid:

 Vector3 pivotToPoint = hit.point - nearestGameObject.transform.position;
 
 float positionX = nearestGameObject.transform.position.x + 
                   Mathf.Round(pivotToPoint.x / nearestGameObject.transform.renderer.bounds.size.x) * 
                    nearestGameObject.transform.renderer.bounds.size.x;
 
 float positionZ = nearestGameObject.transform.position.z + 
                    Mathf.Round(pivotToPoint.z / nearestGameObject.transform.renderer.bounds.size.z) * 
                    nearestGameObject.transform.renderer.bounds.size.z;
 
 Vector3 origin = new Vector3(positionX, 0, positionZ) + Vector3.up * 100;
 
 if (Physics.Raycast(origin, Vector3.down, out hit, Mathf.Infinity, layerMask)) {
     previewGameObject.transform.position = new Vector3(positionX, hit.point.y, positionZ);
     
     previewGameObject.transform.rotation = nearestGameObject.transform.rotation;
 }

Maybe some of you guys have an idea how to solve this, it's driving me crazy.

p.s. Here is some guy with a similar problem, but I'm very lost when in comes to adding the solution into my code.

Comment
Add comment · Show 4
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 Zerebokep · May 26, 2015 at 02:43 PM 0
Share

Anyone? :)

avatar image Zerebokep · Jun 08, 2015 at 03:29 PM 0
Share

I'm willing to pay the one who could fix that problem for me.

http://forum.unity3d.com/threads/very-quick-job-for-experienced-programmer.331697/

avatar image Bunny83 · Jun 08, 2015 at 05:07 PM 0
Share

The question isn't really clear. Do you mean you want to be able to rotate your grid? o.O That sounds a bit strange. If you have two different rotations objects places in one rotation can't possible fit to another in a different rotation.

Do you want just a temporary local grid that rotates with the player? $$anonymous$$eep in $$anonymous$$d as soon as you rotate just a fraction of a degree the grids won't match anymore.

Or do you want a fix grid. If it's a fix grid in worldspace how do you want put rotation in there?

You should be way more specific what kind of behaviour you expect. If you rotate the grid there's no way to match the new grid cells with the cells of the unrotated grid in any way that makes sense.

$$anonymous$$aybe you should add a bit more details for what purpose you want to use that grid snapping?

edit
Oh, another question: Do you want to snap a rotated object to an unrotated object as well? How do you actually rotate your object? The whole picture of your problem has a lot dark spots.

avatar image Zerebokep · Jun 09, 2015 at 12:52 PM 0
Share

For me only the snapping is important. It should work no matter how the target/nearest object's y axis is rotated.

So to answer your question, the grid should always use the nearestObject position/rotation as starting point, so that the rotation of the new object (to spawn) aligns with it as soon the mouse cursor is in range (as shown in my first video).

2 Replies

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

Answer by JoeStrout · Jun 08, 2015 at 04:49 PM

You need to do the snapping in the local coordinate system of the nearestGameObject, instead of in world coordinates. Use nearstGameObject.transform.InverseTransformPoint to convert from world to local, then round to the nearest grid position, then use .TransformPoint to convert this back to world.

Comment
Add comment · Show 4 · 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 Zerebokep · Jun 09, 2015 at 12:46 PM 0
Share

I'm not sure if I understand your suggestion right. So I should use nearstGameObject.transform.InverseTransformPoint with hit.point?

e.g. nearstGameObject.transform.InverseTransformPoint(hit.point);

Then calculating the snapping with the result of that method and calculate it back with nearstGameObject.transform.TransformPoint(snapcalculatedvector3)?

avatar image JoeStrout · Jun 09, 2015 at 01:00 PM 0
Share

Yes, that sounds about right. You've already got basic snapping working -- you just need to do it in the nearestGameObject's local coordinate system, ins$$anonymous$$d of in the world coordinate system.

avatar image Zerebokep · Jun 09, 2015 at 02:49 PM 0
Share

Thanks, that worked. Please send me a pm regarding the payment. :)

avatar image JoeStrout · Jun 09, 2015 at 03:19 PM 0
Share

I'm happy it worked! No need for payment, but if you really want to thank me, you can buy a copy of High Frontier. :)

avatar image
1

Answer by FortisVenaliter · May 22, 2015 at 04:25 PM

Maybe something like this? Untested and may need some modifications, but hopefully it will get you in the right direction.

 Vector3 offset = new Vector3(
     Mathf.Round(pivotToPoint.x / nearestGameObject.transform.renderer.bounds.size.x) * nearestGameObject.transform.renderer.bounds.size.x,
     0,
     Mathf.Round(pivotToPoint.z / nearestGameObject.transform.renderer.bounds.size.z) * nearestGameObject.transform.renderer.bounds.size.z);
 
 offset = nearestGameObject.transform.rotation * offset;
 
 float positionX = nearestGameObject.transform.position.x + offset.x;
 float positionZ = nearestGameObject.transform.position.z + offset.z;
Comment
Add comment · Show 1 · 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 Zerebokep · May 23, 2015 at 11:20 AM 0
Share

Thanks for your answer. It seems like that's the right direction, here is how it looks like with the rotated cube:

https://vid.me/k2tO

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Snapping an object to another 0 Answers

How To Sync Animation To Music or Bpm ? 2 Answers

Stop a line renderer from rotating when new segments are added? 1 Answer

Snap to grid not working for me 1 Answer

Grid Increment by Rotation 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