• 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 IndieStar · Sep 18, 2013 at 07:58 PM · parentfollowtk2d

How to make two or more objects stick together ?

Well I am making a jigsaw like game (2d) I've run over many like question that suggested to parent objects together, well that will make one object follow the other but not vice versa I want to drag either of the two objects and the other move along.

Any help ?

Comment
Add comment · Show 8
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 meat5000 ♦ · Sep 18, 2013 at 08:00 PM 0
Share

Try replicating the movements of one object in the OnCollisionStay() of the other.

avatar image robertbu · Sep 18, 2013 at 08:12 PM 0
Share

For any collection of pieces of two or more, make an empty game object as the parent with all the pieces as children. Dragging would move the parent, empty game object and all the pieces would follow.

avatar image meat5000 ♦ · Sep 18, 2013 at 08:14 PM 0
Share

He also wants to have the case where pulling a child pulls the other children, it seems.

avatar image IndieStar · Sep 18, 2013 at 08:30 PM 0
Share

@robertbu that's what I was trying to do but how to drag the parent, I still drag the children !! while the parent (empty object) is useless @meat5000 that's exactly what I want. maybe I can combine both your answers - make a big parent and replicate the movement of any child into that parent but still the child I moved will double move with the replicated moves.

avatar image meat5000 ♦ · Sep 18, 2013 at 08:49 PM 0
Share

One question. How many objects do you intend to be able to move at once?

Show more comments

1 Reply

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

Answer by robertbu · Sep 18, 2013 at 09:46 PM

I don't know how you are approaching your drag and drop and your combining of pieces. Here is a bit of code as an example of how it may be done. This may not be the best approach for your specific mechanics, but it show a proof of concept. To make the code simple, each visible piece has an empty game object as a parent. If pieces are combined, one of the two parents is abandoned. As mentioned in my comments above, the drag and drop moves the empty parent game object, not the child piece.

 #pragma strict
 import System.Collections.Generic;
 
 private var offset : Vector3;
 
 function OnMouseDown () {
     var v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.parent.position.z - Camera.main.transform.position.z);
     v3 = Camera.main.ScreenToWorldPoint(v3);
     offset = transform.parent.position - v3;
 }
 
 function OnMouseDrag() {
     var v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.parent.position.z - Camera.main.transform.position.z);
     v3 = Camera.main.ScreenToWorldPoint(v3);
     transform.parent.position = v3 + offset;
 }
 
 function Combine(piece : GameObject) {
     var transforms : List.<Transform> = new List.<Transform>();  
     var parent = piece.transform.parent;
     
     var trans = transform.parent;
     
     for (var child : Transform in transform.parent) {
         transforms.Add(child);
     }
     
     for (var child : Transform in transforms) {
         child.parent = parent;
     }
 }

Note that the combine code transfers all siblings to the new parent. This will allow two collections of more than one piece to be combined.

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 IndieStar · Sep 19, 2013 at 10:20 PM 0
Share

@robertbu man you are the best - nice idea (LIST) :)

avatar image Afassolas · Feb 19, 2016 at 10:12 AM 0
Share

Looks great is there a way to make this in C# ?

avatar image meat5000 ♦ Afassolas · Feb 19, 2016 at 10:49 AM 0
Share

You can learn to translate your code very quickly with the scripting reference.

Go through the script method by method.

Google : "Unity On$$anonymous$$ouseDown"

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html and click the C# button in the top right, flicking back and forth until you see the difference.

Some things are tricky to find in the scripting reference but Translations for all Collections including Lists are in this page:

http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type

Once you've done it a couple of times you'll be surprised how simple it is and that a lot of the syntax really isn't much different at all.

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

17 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

Related Questions

make parent of animated game object follow the game object's animation 0 Answers

Object following other object (Parent-like) 2 Answers

How can I make a game object follow an instantiated game object? 1 Answer

Set Position and Rotation with another GameObject as Pivot (Script) 0 Answers

Move one object with another, without parenting it to the object? 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