- Home /
Magnetism in javascript
I'm entering a science competition, and need to model magnetism in Unity. Is there a way of writing this in Javascript?
Answer by Borgo · Jan 13, 2011 at 06:20 PM
You can use joints. Try it!
Well, I want it to be invisible, so it looks like magnetism.
I've never unity before this... sorry if this is ridiculously noobish.
The joints are invisible and looks like magnetism, depends of type.
http://unity3d.com/support/documentation/Components/comp-DynamicsGroup.html
Answer by Bob5602 · Jan 13, 2011 at 06:12 PM
What I would do..
Make 2 game objects, then put some kind of attraction force between them. Something like moving one object toward the other and vice versa, so they appear "attracted." Then make the amount of movement is inversely proportional to the distance (Vector3.Distance) between the two objects. Then, the closer they get, the faster they go!
Answer by efge · Mar 03, 2011 at 09:31 PM
I would suggest to also use Rigidbody.AddForce:
Attach a SphereCollider with a wider radius set as Trigger and a RigidBody to your GameObjects.
Use OnTriggerEnter to start Rigidbody.AddForce in the direction of the other object/collider.
Increase/decrease the added force depending on distance.
(When both GameObjects collide you could use a FixedJoint or parenting.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613095307im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
hit.rigidbody.useGravity problem? 1 Answer
Physics: Is it possible to count the number of colliders hit with OverlapSphere? 1 Answer