Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 halidi665 · Apr 11, 2017 at 08:05 PM · physicsforcechild objectattract

Molecule simulation: make child objects attract each other so parents will stay close together?

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. alt text

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#:

 using UnityEngine;
 using System.Collections;
 
 public class Bond : MonoBehaviour {
 
     public Element myElement;
     public GameObject myPartnerElement;
     public GameObject myPartnerBond;
     public GameObject bondingPoint;
 
     public bool hasPartner;
 
     private BondingRadius bondingR;
     private Rigidbody rigidBody;
 
     // Use this for initialization
     void Start () {
         myElement = GetComponentInParent <Element> ();
         rigidBody = myElement.GetComponent <Rigidbody> ();
         bondingR = GetComponentInChildren <BondingRadius> ();
     }
     
     // Update is called once per frame
     void Update () {
         if (hasPartner) {
             AttractPartner ();
         } 
     
     }
 
     public void FormBond (GameObject otherBond) {
         //        Debug.Log (this.gameObject.name + " FormBond called");
         Bond partnerBond = otherBond.GetComponent<Bond> ();
 
         if (myPartnerBond == null && partnerBond.myPartnerBond == null || myPartnerBond == null && partnerBond.myPartnerBond == this.gameObject) {
             myPartnerBond = otherBond;
             myPartnerElement = otherBond.GetComponentInParent<Element> ().gameObject;
             hasPartner = true;
             bondingR.enabled = false;
             Debug.Log (this.gameObject.name + " bonded with " + otherBond.name);
 //            rigidBody.freezeRotation = true;
             myElement.UpdateBonds ();
         }
 
     }
 
     public void BreakBond () {
         Debug.Log (this.gameObject.name + " broke up with " + myPartnerBond.name);
         myPartnerBond = null;
         myPartnerElement = null;
         hasPartner = false;
         bondingR.enabled = true;
         rigidBody.freezeRotation = false;
         myElement.UpdateBonds ();
 
     }
 
     public void AttractPartner () {
         
         if (Vector3.Distance (transform.position, myPartnerBond.transform.position) > 0.3f) {
             Debug.Log ("Attracting partner");
 //            transform.LookAt (myPartnerBond.transform);
 //            myElement.rigidBody.AddForce ((myPartnerBond.transform.position - transform.position).normalized * 0.005f, ForceMode.Impulse);
             rigidBody.AddForceAtPosition ((myPartnerBond.GetComponent<Bond> ().bondingPoint.transform.position - bondingPoint.transform.position).normalized * 0.005f, bondingPoint.transform.position);
 //            rigidBody.MovePosition (myPartnerBond.transform.position);
 
         }
 //        this.transform.position = this.transform.position + ((myPartnerBond.transform.position - this.transform.position).normalized * myElement.attractionForce);
     }
 }


unity-screenshot.png (288.2 kB)
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 FortisVenaliter · Apr 11, 2017 at 08:10 PM

I would recommend instantiating a FixedJoint component for the bonds to make the atom rigidbodies stick together. That way, you're not fighting the physics engine, you're working in it's rules, so the two rigidbodies will start working in concert.

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 halidi665 · Apr 12, 2017 at 08:48 AM 0
Share

thanks for the tip, it did solve the problem I had with the bonds and atoms getting separated from each other :)) However, it didn't quite work out for joining two bonds. I tried a spring joint for that but it's still buggy and does not behave as I expected... Could it be a problem that each bond instantiates a spring joint onto itself at the moment of bonding and connects it to its partner bond? Because for the connection between atom and bond all I used was a fixed joint on the atom referencing the bond, and leaving the bond itself untouched...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 2D - How to calculate weight/strain? 0 Answers

Ignore Force Imparted By Object 0 Answers

How to calculate force to apply to move object to a certain distance. 1 Answer

Model tips over for no reason 1 Answer

Problems making a glider 3 Answers


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