Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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 StPatrick923 · Jul 19, 2015 at 05:44 PM · parentparent-childdraggingsticking

Sticking, Dragging, and Unsticking Objects

I'm currently working on a game where the user can drag objects around and make them stick together. To do this, I have two objects: the object currently being dragged, and the object that it collides with. Upon collision, the object being collided with sets its parent to the object being dragged, and vice versa.

The idea is that the player can then click on either of those objects and drag them; both objects will move as one, any objects collided with will then join the heap, and they'll all become, effectively, one super object.

The issues I'm having are as follows: 1). If we drag Object A into Object B, B will stick with A and the two will be dragged along as one object. If, however, we start dragging Object B, that one will move on its own, without A. To make matters even weirder, if we then start dragging A again, B will then move along with A, although they aren't physically attached any more.

My "attaching" code is as follows:

 void OnCollisionEnter(Collision collision)
     {
         if (beingDragged == false) 
         {
             GameObject otherObject = collision.gameObject;
             Ball otherObjectScript = otherObject.GetComponent<Ball>();
             
             //only stick to an object we're currently dragging
             if (otherObjectScript.isBeingDragged() == true)
             {
 
                 gameObject.transform.parent = otherObject.transform;
                 otherObject.transform.parent = gameObject.transform;
             }
             
         }
     }

2). I'm looking to implement a hazard that will disconnect some of the objects from the main stack -- not all of them, but some of them. My first instinct is simply set the parents of the afflicted objects to null, but how can I tell the main object they aren't connected any more? Since there's more than one object attached to it, I don't think I can simply say that mainObject.parent = null.

Is there a better way to go about doing all of this?

Any help would be sincerely appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Jul 19, 2015 at 06:09 PM

Did you read http://answers.unity3d.com/questions/701755/how-could-i-make-it-so-that-two-objects-snap-to-ea.html ?

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 StPatrick923 · Jul 19, 2015 at 07:18 PM 0
Share

No, I was not aware of that. Thanks!

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

2 People are following this question.

avatar image avatar image

Related Questions

Display variable from parent in textbox 1 Answer

Child animator component is moving parent despite unchecking "Apply Root Motion" 1 Answer

Parent object is getting generated why ?? 1 Answer

Set the parent of children of a parent,How to set the children of a parent 1 Answer

Why does the child of a game object not rotate to face mouse position when the parent object is moving? 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