Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Find GameObject with Rigidbody by name, then connect HingeJoint to it.

Discussion in 'Physics' started by Nextrevit, Jan 13, 2022.

  1. Nextrevit

    Nextrevit

    Joined:
    Jun 22, 2018
    Posts:
    11
    Hello all, I was not able to find any solid confirmation to my questions.

    Concept: Instantiating a prefab < (I've got this down already). After instantiating, The HingeJoint in the prefab needs to

    1: Search for the rigidbody/GameObject to connect to (In my case, it would be Module CoG, it has a rigidbody.)
    2: Connect to the Module CoG.

    I wrote up a script attempting to solve this, but I keep getting errors. (
    ConnectToRB.cs(33,35): error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Rigidbody')

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ConnectToRB : MonoBehaviour
    6. {
    7.     public Rigidbody myRigidbody;
    8.     public GameObject CoG;
    9.     public Rigidbody joint;
    10.    
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         // Issue below
    15.         if(CoG == null)
    16.         {
    17.             CoG = GameObject.Find("Module CoG");
    18.         }
    19.         else
    20.         {
    21.             Debug.Log("Failed to Connect to Module CoG");
    22.         }
    23.  
    24.         //CoG = GetComponent<Rigidbody>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.        
    31.         HingeJoint hinge = gameObject.GetComponent<HingeJoint>();
    32.         if (hinge != null)
    33.             hinge.connectedBody = CoG;
    34.     }
    35. }
    36.  
    Assistance would be greatly appreciated.
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    hinge.connectedBody is a rigidbody you cannot put there GameObject.
    Joints are connecting Rigidbodies or Rigidbody with "world" if connectedBody is null

    Try this.
    hinge.connectedBody = CoG.GetComponent<Rigidbody>();
     
    Nextrevit likes this.
  3. Nextrevit

    Nextrevit

    Joined:
    Jun 22, 2018
    Posts:
    11
    Thank you, this worked

    However, I've run into a new issue. My object with the Hingejoint connects to the Module CoG, but it's ConnectedAnchors and position change to a really far away location (Like 500 units away).

    At runtime I can edit the ConnectedAnchor values back to 0,0,0 and it'll return to where it should be. I've tried to play around with Auto Configure Connected Anchors to no avail. I'm not sure why it's teleporting so far away.
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Deactivate object that will have joint, position them in proper relative position, add hinge Joint and set connectedBody, anchor and all configurations, set autocconfigure to true.
    And then Activate body.
     
  5. Nextrevit

    Nextrevit

    Joined:
    Jun 22, 2018
    Posts:
    11
    I managed to get the object with the joint to anchor to 0,0,0 when setting AutoConfigure to false (I tried to keep AutoConfigure on but it just created problems, Unity physics are strange)

    Is there any way I can adjust the anchor via slider or button? I've been searching around and can't find any way to do that (Very vague documentation on this). Thanks for the help :cool:
     
    Last edited: Jan 14, 2022
  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Anchor is actually very simple.
    It is just a position in each body local space.
    So when you set anchor on one body and connectedAnchor on other during runtime this two point on those bodies will attract each other.