Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Resolved] Use rigidbody like a magnet?

Discussion in 'Physics' started by Crizz92, Nov 28, 2016.

  1. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    Hi everyone!

    I'm currently working on a project with flying drones. And i'm encountring a problem with some physics tricks.
    My drones use the rigidbody to move with "AddForce()". I want a drone to become a magnet and attract everything around him. The problem is that i can't work with Rigidbody.AddForce() on each object because they will collide with the drone et push him. I have tried to work with Joint and create a "link" between the drone and each object, but it's really tricky and hard to balance. Couldn't find at this moment the best way to achieve this...

    Can anyone give me advice? Or have anyone found a solution?

    A big thanks to you all guys!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,430
    (Untested solution, but it's a reasonable guess)

    You may link each object via ConfigurableJoint with all motion constraints in "Free" mode. Then use the X Drive, Y Drive and Z Drive properties for specifying the behavior of the "magnet". Most likely you'll set constant values for Position Spring and Position Damper (very large spring and very small damper).

    Then you may dynamically calculate a value for the Maximum Force parameter of the drives on each object based on the distance to the magnet. Far objects would receive a small or zero max force, so they won't likely move. Closer objects will receive more and more force (most possibly in a exponential curve with smaller distance, check formulas for magnetic fields) so they would be attracted towards the magnet in a realistic fashion.

    This way the drones would also react realistically to the increased weight as for the "magnetic forces" attracting other objects. You may even define the global maximum force the magnet can apply to the objects, so you could switch it on/ turn it off, or progressively adjust the intensity.
     
  3. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    I'm going to test that!

    Thanks you a lot for the advice, i'm sure the answer is in the joint, but couldn't see the best way to achieve this.

    Thanks you a lot !
     
    Edy likes this.
  4. Crizz92

    Crizz92

    Joined:
    Sep 17, 2015
    Posts:
    44
    So I tested your solution and it works pretty fine!

    Thanks you a lot for the answer and the advice!
     
    Edy likes this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,430
    My pleasure, great to know that it worked!
     
  6. unity_mwF5s3xOpUdXNA

    unity_mwF5s3xOpUdXNA

    Joined:
    Sep 20, 2017
    Posts:
    1
    Hey Crizz92,

    I am working on Roulette game and I wanna achieve to stop ball on any number which I have decided.

    For this, I put an object to every number and trying to activate magnet on specific object that will enforce the towards its direction.

    But somehow when I activate magnet on an object it creates a magnetic field and ball moves surround this field and never reach to object.

    I think your solution for the upper problem can help me. So if you provide me that solution work or give any suggestion that will be so much helpful for me.

    Thanks a lot!