Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 thunderball001 · Aug 14, 2015 at 01:10 PM · c#objectforcemagnetcharge

How would you make an object behave like a magnet?

I have made a 2-d version of this by adding a CircleCollider2D to a empty object. Added a PointForceComponent script to the empty object, then adjusted its properties. For my initial test, I used the following values in the inspector:

Non Linear: True

Force: 5

Linear Drag: 0.1

Angular Drag: 0

The tracked Object is a simple circle sprite

But the only problem is that it only affects game objects that have the Rigid Body 2D and Collider 2D components. I would like to adapt this idea from 2D into 3D where by one object is positively charged and the other is negatively charged. Many Thanks. :)

Comment
Add comment · Show 6
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 UnitedWeFall · Aug 14, 2015 at 01:36 PM 0
Share

This is simple enough to script, but it's not exactly clear what you're trying to achieve from the question. Do you just want 2 3D RigidBody objects to be attracted to each other, or do you want to properly simulate magnets with poles and attraction and repulsion?

Do you want to pull in all objects within a radius or do you just want interaction between 2 specific objects?

avatar image thunderball001 · Aug 14, 2015 at 04:11 PM 0
Share

I was planning to 'properly simulate magnets with poles and attraction and repulsion'. So like one magnet is (+) and the other is (-).

avatar image UnitedWeFall · Aug 14, 2015 at 04:19 PM 1
Share

Well that isn't how magnets actually work. $$anonymous$$agnets all have both a North and South pole. Would you want to simulate this?

avatar image Cybless · Aug 14, 2015 at 04:29 PM 0
Share

Just an idea! I think you need a "GravityAttractor" script on your object and have a "GravityBody" script on your player, then your player will get attracted to the object maybe?:)

avatar image thunderball001 · Aug 15, 2015 at 12:25 PM 0
Share

Even if magnets do not work like that. Would there be some way to have one object (+) and an other (-).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Fattie · Aug 15, 2015 at 12:43 PM

Assuming you want object B to always be attracted to object A.

Simply, add a script to B. Have with one line of code which causes B to feel force in the direction towards A.

The strength of the magnetic field is (ROUGHLY) 1 divided by the distance between the two squared.

Good luck.


To increase the general frisson of your ongoing quest for knowledge,

• learn that the direction from A to B is (B-A)

• find the Vector2 or Vector3 documentation, look for "distance" related functions

• learn about normalizing a vector to length one, the purpose and mechanisms of this

• learn how to apply force to an object, the subtleties of doing so each frame

Enjoy!

Comment
Add comment · Show 3 · 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 thunderball001 · Aug 15, 2015 at 12:54 PM 0
Share

Thank you Fattie. Even though I understand what you mean, the only problem is that I would not know how to create 'one line of code which causes B to feel force in the direction towards A' in a 3-D format.

avatar image Fattie · Aug 15, 2015 at 01:35 PM 0
Share

You're welcome, thanks for the thanks. I've added some more info to help on your quest.

avatar image thunderball001 · Aug 15, 2015 at 06:33 PM 0
Share

ok, I get that. That should help me understand what I need to do. Really helpful. thank you.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to let an object drive on a road? 1 Answer

Simple way to make an object go up and down?(Y axis) 1 Answer

Serialization - Variables won't change on original construction 1 Answer


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