Search Unity

Make child objects attract each other so parents will stay close together?

Discussion in 'Physics' started by halidi665, Apr 11, 2017.

  1. halidi665

    halidi665

    Joined:
    Apr 11, 2017
    Posts:
    1
    Hey guys, I'm making a simple molecule simulation game. So far, I have single atoms wander around until they come across another single atom, then approach each other and form a bond at their respective bonding points, meaning that from this moment on, their bonds (which are child objects to the atom game object) should stick together (attract each other) so the bond won't be broken by the atoms drifting too far away from each other. This would also stabilize molecule chains. And I cannot for the life of me make this work.
    Here's a screenshot: https://www.dropbox.com/s/8ooppkb4ryn5d9u/unity-screenshot.png?dl=0

    The atom game objects have rigid bodies, the bond game objects don't. the Bonding radius and Action radius objects are simply colliders which act as triggers. The bonding point game objects were me trying to make this work by having a transform at which the two bonds should act out their attraction forces after the FormBond() method had completed. Any help is greatly appreciated!

    This is my Bond Script in C#:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bond : MonoBehaviour {
    5.  
    6.     public Element myElement;
    7.     public GameObject myPartnerElement;
    8.     public GameObject myPartnerBond;
    9.     public GameObject bondingPoint;
    10.  
    11.     public bool hasPartner;
    12.  
    13.     private BondingRadius bondingR;
    14.     private Rigidbody rigidBody;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         myElement = GetComponentInParent <Element> ();
    19.         rigidBody = myElement.GetComponent <Rigidbody> ();
    20.         bondingR = GetComponentInChildren <BondingRadius> ();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.         if (hasPartner) {
    26.             AttractPartner ();
    27.         }
    28.  
    29.     }
    30.  
    31.     public void FormBond (GameObject otherBond) {
    32.         //        Debug.Log (this.gameObject.name + " FormBond called");
    33.         Bond partnerBond = otherBond.GetComponent<Bond> ();
    34.  
    35.         if (myPartnerBond == null && partnerBond.myPartnerBond == null || myPartnerBond == null && partnerBond.myPartnerBond == this.gameObject) {
    36.             myPartnerBond = otherBond;
    37.             myPartnerElement = otherBond.GetComponentInParent<Element> ().gameObject;
    38.             hasPartner = true;
    39.             bondingR.enabled = false;
    40.             Debug.Log (this.gameObject.name + " bonded with " + otherBond.name);
    41. //            rigidBody.freezeRotation = true;
    42.             myElement.UpdateBonds ();
    43.         }
    44.  
    45.     }
    46.  
    47.     public void BreakBond () {
    48.         Debug.Log (this.gameObject.name + " broke up with " + myPartnerBond.name);
    49.         myPartnerBond = null;
    50.         myPartnerElement = null;
    51.         hasPartner = false;
    52.         bondingR.enabled = true;
    53.         rigidBody.freezeRotation = false;
    54.         myElement.UpdateBonds ();
    55.  
    56.     }
    57.  
    58.     public void AttractPartner () {
    59.      
    60.         if (Vector3.Distance (transform.position, myPartnerBond.transform.position) > 0.3f) {
    61.             Debug.Log ("Attracting partner");
    62. //            transform.LookAt (myPartnerBond.transform);
    63. //            myElement.rigidBody.AddForce ((myPartnerBond.transform.position - transform.position).normalized * 0.005f, ForceMode.Impulse);
    64.             rigidBody.AddForceAtPosition ((myPartnerBond.GetComponent<Bond> ().bondingPoint.transform.position - bondingPoint.transform.position).normalized * 0.005f, bondingPoint.transform.position);
    65. //            rigidBody.MovePosition (myPartnerBond.transform.position);
    66.  
    67.         }
    68. //        this.transform.position = this.transform.position + ((myPartnerBond.transform.position - this.transform.position).normalized * myElement.attractionForce);
    69.     }
    70. }
     
    Last edited: Apr 11, 2017
  2. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    I think Fixed Joints would work for this.