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
0
Question by Rhithik · Nov 29, 2012 at 04:57 AM · objectscoinsmagnetmagnetic

Magnetic object to make coins move to object

I'm trying to make an object that is moving at various speeds to be magnetic and if it gets close enough to a coin it will suck it in towards the object.

What I have right now works kind of:

 distance = Vector3.Distance(transform.position, junk.transform.position);
             
 if(distance <= 35 && !destroySequence) {
     destroySequence = true;
 }
 if(destroySequence && !moving) {
     moving = true;
     iTween.MoveTo(transform.gameObject, iTween.Hash("x", transform.position.x, "y", junk.transform.position.y, "z", junk.transform.position.z,"time", 1.0f, "oncomplete", "destroyCoin"));
 }

It moves the coin to my object which is sometimes moving a very fast rates and the object will move past the position that the coin is suppose to move to so the object is usually in front of the coins when they reach the position.

So what I tried is if the coins hit the object, that it destroys the coin. But what I found is the coin almost never hits the object. I've been doing a lot of searching on the internet and can't find what I am looking for. I also tried lowering the time on the iTween but that makes it so quick you can't even see it, and it still has that same issue above.

If you don't understand think of Temple Run and the magnet power-up and how it sucks in the coins. Something like that.

Thanks.

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

2 Replies

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

Answer by fafase · Nov 29, 2012 at 06:55 AM

Make your coin a Rigidbody and try AddForce to the coin. So you need to try first because I did not... The idea is that you have a trigger sphere around the coin. When the magnet gets in, you start calculation. You already have a reference to the transform of your magnet.

What you do is get the magnitude of the vector between the coin and the magnet. Use a linear equation to define an index. Then you pass all the required variable to AddForce. This way, as you get closer to the magnet, magnetField.magnitude gets smaller->index gets bigger->the force gets bigger. You might have to change the force value as well

 bool inside;
 Transform magnet;
 float radius = 5f;
 float force = 100f;
 void Start(){
    magnet = GameObject.Find("Magnet").GetComponent<Transform>();
    inside = false;
 }
 void OnTriggerEnter(Collider other){
    if(other.gameObject.tag =="Magnet"){
    inside =true;
 }
 void OnTriggerExit(Collider other){
    if(other.gameObject.tag =="Magnet"){
       inside =false;
 }
 
 if(inside){
    Vector3 magnetField = magnet.position- transform.position;
    float index = (radius-magnetField.magnitude)/radius;
    AddForce(force*magnetField*index);
 }
Comment
Add comment · Show 6 · 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 Rhithik · Nov 30, 2012 at 01:36 AM 0
Share

Wow thank you very much! This is exactly what I needed.

avatar image Rhithik · Nov 30, 2012 at 02:25 AM 0
Share

I seem to have another issue now. It works with this when it goes at relatively slow speeds. But when my object that is the magnet starts picking up speed, the coins miss the magnet and just circle it and picking up speed like crazy. I tried doing if the distance < 5 (an example) or whatever that it deletes it. But it seems to just go flying past it and ignore that. Any ideas on how to get this working on high velocity objects? Thanks though!

avatar image fafase · Nov 30, 2012 at 06:32 AM 0
Share

is if (inside) in the FixedUpdate? Also, I would rather increase the force ins$$anonymous$$d of altering distance.

avatar image Rhithik · Nov 30, 2012 at 10:04 PM 0
Share

Yes it is inside FixedUpdate and I did increase the force and it still happens to do it although not as often. It would just circle the object faster and faster ins$$anonymous$$d of actually going to it. Thanks for all the help so far though.

avatar image fafase · Dec 01, 2012 at 08:13 AM 0
Share

Ok, what you describe actually makes sense to me. $$anonymous$$aybe you want to add some drag and angular drag. Simply said, drag is the air resistance and angular drag is the friction. Your environment might be somehow ideal with no friction at all, so nothing restrains the forces adding together and your coin keeps on accumulating forces.

Air resistance / drag increases with the velocity of the object so the faster your coin goes, the more opposite force it gets.

Friction / angular drag is a little similar until a certain critical velocity depending on the system. Think of your car, at low speed, you brake and the car stops properly, but at high speed, the wheels lose grip and you start skidding.

So my tip would be to start adding values to drag and angular drag and you might see your coin acting in a more physical way.

You could look there http://unitygems.com/mistakes1/. the video shows in action what those variables do.

Show more comments
avatar image
0

Answer by Maulik2208 · Nov 29, 2012 at 06:34 AM

Try to associate it with *time.deltatime just like we are doing in Rotation like stuff.......and i am not so sure But your problem will be solved by that.....

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

12 People are following this question.

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

Related Questions

How to remove duplicate values from a list 1 Answer

Magnetic Race Track? How to stick to a tubular track? 1 Answer

Gather objects in one point 1 Answer

Magnet Power Up,moving the coins towards the player 1 Answer

Character attract from magnet object 0 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