• 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 NutellaDaddy · May 06, 2014 at 03:30 AM · raycasttransformtagssnapping

How could I make it so that two objects snap to eachother?

I'm planning on making two snap points snap together by using raycast to get their tags and an if statement to tell which one I'm holding. The problem is that the snap points are parented to the object that should move with them ,but if I only snap the children to the other snap points transform will the parent object follow? If not how could I do this?

Comment
Add comment · Show 2
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 getyour411 · May 06, 2014 at 03:38 AM 0
Share

To the first, moving child transform will not move parent transform. Not knowing your setup, I don't know if it will work to just do transform.parent.pos = newVec3

avatar image NutellaDaddy · May 06, 2014 at 03:45 AM 0
Share

If I can do the math to get it so that the parent follows the snap point the same amount than maybe. I also have to account for all of the other snap points that I have. I am using a basic cube for testing purposes so I have 6 snap points under one parent(the cube). I trying to get something like $$anonymous$$SP if that helps at all. This parent child thing is really bugging me. If anyone has an equation that will make the parent follow the snap point(where it translates to) would be very helpful. Not to the exact point, just so that all the other children of the cube,and the cube, fit up the same way. Thank anybody for any help!

1 Reply

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

Answer by robertbu · May 06, 2014 at 09:42 AM

This question has come up a couple of times in the past year in relation to jig-saw puzzles, where the the developer wanted to allow the user to put a couple of pieces together, and the drag those pieces by clicking on either of the two pieces. My suggestion to one of those questions was to establish a parent of any pieces that are snapped together. So if you snapped two pieces together, you would have an empty game object as a parent, two pieces as children, and each of those pieces would have (in your case) six snap points. If more individual pieces are snapped into the first two, they get the same parent. If the user drags any piece, then the parent is moved and all the children follow. If two multi-piece sections are clicked together, one parent is abandoned, and all the resulting pieces are given same parent.

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 NutellaDaddy · May 06, 2014 at 09:18 PM 0
Share

Could you show me some sample code as to how I could do this?

avatar image robertbu · May 06, 2014 at 10:28 PM 0
Share

Sample code for a full solution is beyond the scope of a Unity Answer. That is, you have drag and drop code, proximity code, perhaps rotation code, as well as the the parenting code. But here is a bit more information dealing with the specific issue of parenting (the core issue of your question):

Imagine your two pieces are correctly positioned with respect to each other and you now want to combine them. Each 'piece' may be one piece or multiple pieces combined. The logic might look like:

 function CombinePieces(piece1 : Transform, piece2 : Transform) {
     if (piece1.parent != null) {
          if (piece2.parent != null) {
              Destory(piece2.parent.gameObject, 0.5);
          }
          piece2.parent = piece1.parent;
     }
     else {
        if (piece2.parent != null) {
             piece1.parent = piece2.parent;
         }
         else {
             var trans = new GameObject().transform;
             piece1.parent = trans;
             piece2.parent = trans;
         }
     }
 }

I'm not sure how you are moving your pieces in the scene. But any time you move objects or use the position of the object in a calculation part of a move, you need to check the transform.parent. If it is not-null you use the transform.parent ins$$anonymous$$d of the transform for a calculation.

avatar image NutellaDaddy · May 07, 2014 at 12:11 AM 0
Share

Thank you

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Move object along ray cast using object's AddForce or velocity but not transform 1 Answer

How to raycast always in direction of a object? 3 Answers

Transform.LookAt Seems To Cause Issues 1 Answer

Adding components to objects that have the same tag 1 Answer

Rotate raycast origin with gameobject 0 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