Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Attaching objects to each other? (SOLVED)

Discussion in 'Getting Started' started by koskok2695, Dec 4, 2015.

  1. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Hello there guys,

    I've recently downloaded and started messing with the amazing Unity Engine and of course, I have s**tloads of questions about it. So as I heard there's an amazing community for it as well ,I thought I'd give it a try!

    Let's cut to the chase ! I started by making a physics script for simulating a real physics force omitted by Unity. And as I wanted to test my script ,I run into this problem: How can I attach objects to each other solidly and non-elastically ,like they are one? I found the Fixed Joint ,but it became obvious that any forces acting upon an objects complex joined with Fixed Joints can actually "bend" the Joints ,while if the force is large enough ,the Joint will "go nuts" sending the objects flying all around the place.

    Is there any way in which I can directly associate one object's movement with another ,so that they really act like they are one single entity? I saw something about parenting being able to accomplish that in the documentation ,but I'm afraid that if one object just inherits one other object's movement properties ,collisions with that child object or forces acting upon it will not affect it (as it just inherits properties) ,nor its parent.

    So is there any official or maybe sneaky way to get around this problem ? Can I fix objects together tightly ,yet keep their realistic physical behavior ?

    Thank you in advance ! All help attempts are greatly appreciated !:D

    P.S. Is it just me ,or are joints in Unity rather unstable ?
     
    Last edited: Dec 4, 2015
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    you can have a single rigidbody with multiple colliders, that will give you the results you want.

    Put a rigidbody component on the parent object and then have as many colliders as you want as children, or even put all the colliders on the same object if you wish.

    http://docs.unity3d.com/Manual/class-Rigidbody.html
     
  3. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Thanks for the reply ! I've seen that in the documentation before and it rises some questions to me. WilI I be able to keep the children's visual meshes ? How can I have colliders identical to the visual meshes ,like each object does by default ? If I want to split the object back to its component objects ,will I be able to do it ? I made a fluid dynamics script that relies on the forward vector and velocity of a flattened box object ,that I attach to any flat box I want to turn into a fin ,will it work afterwards ?

    To be very specific : I have that script I said and applied it on 2 flat boxes. I placed them on opposite sides of a cylinder's end ,in opposite angles ,essentially making a rotor ,or propeller or whatever you'd like to call it. Then I added Fixed Joints on the boxes and fixed them to the cylinder.
    I placed the contraption at some height and dropped it. It spun as it fell just like wanted and expected. But when the cylinder and boxes touched the ground together ,the joints bent. When I added a torque script to the cylinder so it spins with my input so that I would make the rotor fly ,the torque would either be insufficient to spin the rotor quickly ,or the joints would "explode" from the excessive torque of the cylinder and the resistance of the fins. That's what I'm trying to fix. So I'm afraid my script would be rendered useless if I had one only rigidbody.

    Here's a screenshot :
     

    Attached Files:

    • FDE.png
      FDE.png
      File size:
      492 KB
      Views:
      1,281
    Last edited: Dec 4, 2015
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    "WilI I be able to keep the children's visual meshes?"
    Yes.

    "How can I have colliders identical to the visual meshes ,like each object does by default?"
    Exactly the same way. Parent the objects with MeshFilters, Renderers, and MeshColliders to a GameObject that has a rigidbody. All the meshcolliders will be treated as a single rigidbody. I'll also point out that I believe mesh colliders have to be 'convex' to be used as a rigidbody, I might be wrong but you won't get good results as non convex mesh colliders aren't very stable or accurate.

    " If I want to split the object back to its component objects ,will I be able to do it?"
    Yes, Destroy the compound object and create new objects for each separate part.

    "I made a fluid dynamics script that relies on the forward vector and velocity of a flattened box object ,that I attach to any flat box I want to turn into a fin, will it work afterwards?"
    I don't know, I didn't write that script.

    "That's what I'm trying to fix. So I'm afraid my script would be rendered useless if I had one only rigidbody."
    You'll still be able to apply torque and forces on the rigidbody based on the position and orientation of each of it's compound colliders using functions like:

    rigidbody.AddForceAtPosition( );
    http://docs.unity3d.com/ScriptReference/Rigidbody.AddForceAtPosition.html

    It should give you pretty much the same effect.
     
  5. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Thanks again ! It seems like I may be able to use parenting in the end. And as about the script ,what I meant is that the "input" data the script uses to create the force is based on the very rigidbody it is attached to ,so in that rotor you saw ,if I had one only rigidbody ,I wouldn't have the 2 forward vectors and 2 velocity vectors I need for the 2 boxes.If I can't get the individual velocities and forward vectors for each box ,is there at least a way in which I could "measure" these 2 vectors at a point of the object complex ? I hope I ain't confusing you too much.

    P.S. Could you point me to a tutorial for this parenting method you mentioned ? I understand that it is done via scripting ? And it shouldn't be easy for a complete n00b like me. Also ,can I "detect" which object's collider is hit in the objects complex when a collision occurs ? Like ,if I had a tiny box parented to a huge box and only when the tiny box's collider portion would be hit ,the 2 objects would get unparented.
     
    Last edited: Dec 5, 2015
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Have you had a chance to run either "Roll-a-ball" or "Space Shooter" in the "Learn" section of the website?
    http://unity3d.com/learn/tutorials/modules

    These will give a basic insight into how Unity works.

    The Parent/Child relationship is a very basic and very vital concept. Any GameObject you make a child of another GameObject will have it's Transform values relative to the parent GameObject. You can make a GameObject partent/child structure in code (http://docs.unity3d.com/ScriptReference/Transform.SetParent.html), but this is more commonly done by placing one GameObject into another GameObject using Drag and Drop.

    I'd suggest going through some of the more basic tutorials here:
    http://unity3d.com/learn/tutorials/topics/interface-essentials

    ... including:
    http://unity3d.com/learn/tutorials/.../the-hierarchy-and-parent-child-relationships

    ... which will help explain things better.

    There are also relevant sections in the User Manual:
    http://docs.unity3d.com/Manual/Transforms.html

    ... look there for "Parenting".

    BUT: If you've not read up on this, you may want to quickly run through the beginning of the manual, looking at Unity basics, especially the page on the Hierarchy, which also include a section on Parenting, as this is where you'd be dragging one GameObject into another to make a "GameObject Family":

    9edf051c74ac232e76e50179a031a04c.png
     
  7. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Well if you did look at all things said in this thread ,you would know I did in fact look into the basics as I already know how "simple parenting" works. All of these tutorials and explanations are extremely superficial and give me no deep insight to the parenting concept. And exactly because of that ,I think "simple parenting" wouldn't fit my purposes the way these tutorials present it. I want to preserve each and every object on its very own ,be able to split the object group any time I want ,have physics treat the object group as one single thing and on top of all that , be able to receive feedback from each and every object in the group like the 2 forward vectors I mentioned. Can parenting do all of that ,even if not "easily" ? If not ,I'm not interested ,if yes ,then how ? Again ,these tutorials are just scratching the surface ,nothing that helps me there. That's why I want to learn from the pros by asking in the forums.;)So if you know more than these tutorials say ,give me a hand or point me to someone that could.

    P.S. Parenting the way the documentation implies ,my child objects don't collide and don't affect the movement of the parent ,only the parent does ,but I want them to affect each other ,like in real life.
     
    Last edited: Dec 7, 2015
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    I gave you all the answers already...

    You can have a compound object, multiple colliders on a single rigidbody. If you want to split and combine objects at runtime then you will need to create and destroy objects are runtime. e.g.

    Combining two objects:
    Create two separate objects, when a collision is detected, Instantiate a new object with a single rigidbody on the root node, under that node add children with colliders that have the exact same dimensions and position as the two objects that collided. You'll also need to combine the velocities and masses of the two initial objects and set that on the new combine compound object.

    Splitting two objects (do the reverse of the above):
    Create a two objects, each with a rigidbody on the root node. Add gameObjects with colliders under each of those new objects that match the total of the original object. workout the velocities that each of these objects should inherit from the original object and set it on the new rigidbodies.

    To detect collisions for a particular Collider in a compound object, simply add a script with a OnCollisionEnter( Collision collision ) function defined on the gameObject with the collider. If there are multiple colliders on the same object you will not be able to differentiate which was collided with so make sure each collider is on a separate gameObject.
     
    ThatRobRobinson and koskok2695 like this.
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    These are the people who make our physics engine:
    http://www.nvidia.com/object/physx-9.14.0702-driver.html

    Perhaps you'd like to ask them to rewrite it for you?

    When they have done that, please PM us and we'll incorporate it into our product.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Sorry - that was a bit "trolley", but people here are trying to help you and a better tone would be appreciated.

    Rigidbody physics is based on the concept of, well, a rigidbody:
    https://en.wikipedia.org/wiki/Rigid_body

    The Unity implementation of the Rigidbody follow these lines:
    http://docs.unity3d.com/ScriptReference/Rigidbody.html

    There is a lot to understanding how this works and the limits of a pseudo-physics system that can be effective in a run-time environment.

    You can affect physics by manipulating things like the rigidbody's mass:
    http://docs.unity3d.com/ScriptReference/Rigidbody-mass.html

    ...center of mass:
    http://docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html

    ...etc.

    To do complex shapes and constructs in a rigidbody physics environment, you will need to make some concessions and work within the bounds of the system. You can dynamically change the parents of a GameObject. You can dynamically add and remove Rigidbody Components from GameObjects. Perhaps you need to write a method that looks that two objects that come into contact with each other, and based on the situation -
    • Breaks off a piece of the main object - and removes a child GameObject from the family, adds a Rigidbody and gives it an initial velocity and rotation.
    • Adds the colliding object to it (like an asteroid hitting a moon) - and adds the two rb.velocities together, adds the mass, and makes the impacting GameObject a child of the family by removing the Rigidbody Component from the GameObject that becomes the child.
    Now - there are also physics joints, but you have to realize that these are joints between two GameObjects with parameters that you can set. If the forces and torque go beyond the designed maximum amounts the joints will break. They are designed to. If you try to hack them so they don't break, then they will have unpredictable behaviour.

    I would suggest that you learn what the PhysX system can do for you, rather than asking the PhysX system to do what you want.
     
  11. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    I think I've been kind of misunderstood here ,but after all it may be my fault. I don't mean to offend or troll anybody and I most certainly didn't mean to sound ironic or underestimating. As I mentioned on the very beginning of my thread ,I already aknowledge the quality of this community and the helping character of everyone who takes time to reply to my post. I see no enemies. If I actually sounded offensive ,trolley or anything else negative ,I'm truly sorry. I'm just a n00b seeking too deep answers for very specific things ,with poor knowledge of the engine. By no means do I want anything in this incredibly flexible engine changed or rewritten for my tastes and likes ,I'm not that ignorant and egoistic to require anything from these guys that probably worked extensively hard to make it perform as well as it does. All I want is to comprehend it ,to grasp it and master it and if and when I finally get to something exciting and innovative ,share it with everybody else here. Again ,my sincere apologies ,I wasn't even remotely aware of my speech being upsetting. I won't bother you anymore.
     
    Last edited: Dec 10, 2015
  12. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Thank you a 3rd time for the feedback ,I understand the general idea you present and it's probably what I'm seeking for. I still need to see this whole concept in action though ,as I haven't ever practiced parenting via scripts nor I've ever seen how to create compound colliders. I'm going to do some research on this subject and see what I'll find. If you could suggest me someone who makes detailed tutorials ,preferably in videos ,I'd appreciate that a lot. My apologies for being persistent and stubborn like a little child ,it's just my confusion from the initial contact with the engine.Take care.:)
     
  13. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    There's really not a lot to it:
    Parenting in script:

    Code (csharp):
    1.  
    2. transform.parent = someOtherObjectsTransform;
    3.  
    Compound colliders are not complicated either, all they are is a GameObject with a single Rigidbody Component with multiple Collider Components either on the same GameObject or on child GameObjects. In your case you'll want the colliders on the children of the GameObject with the Rigidbody Component so that you can detect the collisions of each collider in the 'compound' Rigidbody.
     
    koskok2695 likes this.
  14. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    I'd advise you read through the Unity Manual again and simply play around and try things out. All these concepts are well documented in there.
     
  15. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    Excellent , parenting works like a charm ! I tested it by parenting 2 3D Objects and adding a Rigidbody to the parent. However ,about the OnCollisionEnter script you mentioned ,it doesn't work ,I made this debug script just to see if it works:

    #pragma strict

    function Start () {}

    function OnCollisionEnter (collision : Collision) {

    Debug.Log("Collision Detected !");

    }

    When I added it to the parent and let the compound of the 2 objects fall to the ground ,it showed the debug message I wrote ,no matter which object's collider was hit ,when I added it to the child and did the same test ,nothing happened ,as if it couldn't detect the collision involving it's collider. Still ,I'm very happy that I started out somewhere ,thanks !

    P.S. Note that I'm working with JavaScript ,if that makes for any practical difference from using C#.
     
    Last edited: Dec 11, 2015
  16. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    Right sorry, I'm sure it use to work that way... maybe I am mistaken though... But you're right, this OnCollisionEnter is only called on the object with the Rigidbody.

    To test to see which collider was impacted, you have to iterate over the contacts in the collision. Each contact has the property 'collision.contacts[index].thisCollider' which indicates the collider in the compound object that was impacted.
     
    Last edited: Nov 30, 2016
    koskok2695 likes this.
  17. koskok2695

    koskok2695

    Joined:
    Nov 23, 2015
    Posts:
    13
    11/10! I searched a little bit about this method with contacts you said ,incorporated it along with the booleans and rigidbody / parenting structures for my needs and finally ,it works FLAWLESSLY! Thanks a ton for all your time ,patience ,responsiveness and instructional clarity throughout this thread! I got EXACTLY what I was looking for! Good luck in all of your future endeavors and keep up the good work helping n00bs like me! Cheers! :D :D :D
     
    Antony-Blackett likes this.