Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Dino · Oct 29, 2010 at 04:39 AM · attachmagnet

How do i create a Magnet?

Im a total newb, but I want to know how to make a magnet, so that it will pick up objects close to it, like a scrap metal magnet.

I need to know how/what scripting needs to be done in javascript, or if someone can provide me with one. I have the model for the magnet, and various cubes which need picking up. Then, with the help of the magnet, I want to be able to move it around the environment, in order to drop the box into a container.

I was thinking of using some form of parenting, or perhaps applying gravity to the magnet, or some other technique. Also, how to transform the Magnet to make it go up and down using different keys to WASD, as thats already being used, preferably by using the space bar (hold space to make it go up, let go of space to make it go down).Any help will be greatly appreciated.

Thanks!

Comment
Add comment · Show 1
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 · Oct 29, 2010 at 05:04 AM 0
Share

$$anonymous$$oving the magnet should probably be a separate question, but there are certainly other questions / tutorials about moving 'characters'.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by · Oct 29, 2010 at 04:52 AM

Broadly, I would:

  • Use OverlapSphere to return an array of all the colliders inside a radius around the magnet,
  • Iterate through the array, and use Rigidbody.MovePosition to move the objects towards the magnet,
  • Determine the positions by Lerping from the current position and desired position (so the further from the magnet, the less it moves towards the magnet)

You could also parent the objects to the magnet when they collide, and ignore any children of the magnet when you do your pass through the array to lerp their positions.

Comment back if you have trouble with these concepts, and I can attempt to explain in further detail!


After posting this, I found a similar question for magnet code, which approaches this problem in a very similar way, but has provided code as well.

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

I need to be able to get an object to attach to another one on mouse hold 0 Answers

Attaching an object to a bone? 3 Answers

3ds max Attach poly problem with Occlusion Culling 0 Answers

attaching camera after an event occurs 1 Answer

Applying a texture to a sphere 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