Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 drpelz · Apr 03, 2018 at 01:40 PM · c#unity 2dgame development

Magnet effect to draw coins,Magnetic effect to draw coins

Hello,

I have a space ship and some coins in my game. When I move my space ship towards the coins I want to have a magnetic effect so that the spaceship draws all the coins in. The code below does not work in my case. I have a circlecollider2d attached to a child of all my coins. The idea is when any coin comes nearer and nearer towards my spaceship the spaceship draws all of the coins in. But it's not working. Any help, please?

This code is attached to the coin.

 public GameObject attractedTo;
 public float radius;
 public float force;
 
 private bool inside;
 private Transform magnet;
 
 private Rigidbody2D rb;
 
 
 void Start() {
     rb = attractedTo.GetComponent<Rigidbody2D>();
 
     magnet = GameObject.Find("Magnet").GetComponent<Transform>();
   
     inside = false;
 }
 
 void FixedUpdate () {
     if(inside) {
         Vector3 magnetField = attractedTo.transform.position - magnet.position;
         magnetField.z = 0;
         float index = (radius - magnetField.magnitude) / radius;
         rb.AddForce(force * magnetField * index, ForceMode2D.Force);
     }
 }
 
 void OnTriggerEnter2D (Collider2D other) {
     if (other.gameObject.tag == "PlayerShipTag") {
         inside = true;
     }
 }
 
 void OnTriggerExit2D (Collider2D other) {
     if (other.gameObject.tag == "PlayerShipTag") {
         inside = false;
     }
 }
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 Harinezumi · Apr 03, 2018 at 03:41 PM 0
Share

Basic questions: is your radius greater than 0? Is the collider on your coins set to trigger? $$anonymous$$ore importantly, what doesn't work? Do the coins not move at all, or they move, but not in the right direction?

Also, I just noticed an issue: index will be negative when outside radius, so outside the radius objects will be pushed away ins$$anonymous$$d of attracted. Of course, this is not an issue if radius is the same size as the trigger collider, but it could become a bug.

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by toddisarockstar · Apr 03, 2018 at 09:18 PM

multiplying the inverted distance by speed would make the coin move towards faster as it gets closer. attach this to your coin

     void Start () {
     ship = GameObject.Find("name of spaceship here").transform;
     }
     
 
     void Update () {
         float dis = Vector3.Distance (ship.position, transform.position);
         if (dis < 30) {
             float speed = 30-dis;
             speed = speed *Time.deltaTime*.5f;
                        transform.position = Vector3.MoveTowards (transform.position, ship.position, speed);}
     }
Comment
Add comment · Show 2 · 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 drpelz · Apr 04, 2018 at 12:26 PM 0
Share

Thanks. It works but my game runs very choppy now (slow). Is there an alternative available (ins$$anonymous$$d of Vector3.Distance)?

avatar image Harinezumi drpelz · Apr 04, 2018 at 12:40 PM 0
Share

I thought that solution would not be efficient enough. You should not check for distance in Update(), especially if multiple objects have the script. I will try to write up a answer.

avatar image
1

Answer by Vilhien · Apr 03, 2018 at 11:18 PM

I do something similar to my fuel drops for my ship.

This script is attached to your (coin) that has a rigidbody component. Obviously you might want to set your distance a bit closer in your case ;)

 public class gather : MonoBehaviour 
 {
         Transform player;
         public float distance;
         Rigidbody move;
         // Use this for initialization
         void Start () {
             move = GetComponent<Rigidbody>();
             player = GameObject.FindGameObjectWithTag("Player").transform;
         }
         
         // Update is called once per frame
         void Update ()
         {
             distance = Vector3.Distance(transform.position, player.transform.position);
             if (distance < 115900)
             {
     
                 move.AddForce((player.transform.position - transform.position) * (16));
             }
     
         }
     }
 


Comment
Add comment · Show 2 · 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 drpelz · Apr 04, 2018 at 01:01 PM 0
Share

Thanks for your solution! I changed the line with the Vector3.Distance to distance = (this.transform.position - player.transform.position).sqr$$anonymous$$agnitude; because it's much faster than Vector3.Distance. It's working now!:)

avatar image Vilhien drpelz · Apr 04, 2018 at 10:23 PM 0
Share

awesome, I will try sqr mag as well. I have heard its faster but I never adopted it into my code. Glad to hear it worked and feel free to contact me anytime, I'm relatively new by about five months but happy to brainstorm or help where I can.

avatar image
0

Answer by tormentoarmagedoom · Apr 03, 2018 at 03:48 PM

Do you know about Vector3.Distance (position1, position2) ?

:D !!

Bye!

Comment
Add comment · Show 2 · 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 Harinezumi · Apr 04, 2018 at 07:27 AM 0
Share

Distance is already considered when using magnetField.magnitude, magnetField being attractedTo.transform.position - magnet.position.

avatar image drpelz · Apr 04, 2018 at 01:03 PM 0
Share

$$anonymous$$y game ran very choppy when I tried this out. But thanks for the hint anyway.

avatar image
0

Answer by Harinezumi · Apr 04, 2018 at 12:57 PM

As written above, this would be my solution. You need a Collider2D set to trigger on the object you want this object to be attracted to and set its tag to "PlayerShipTag". I would also suggest to store magnetStrength and magnetRange on the player, to avoid attracting objects differently, but that is a different topic.

 public class MagnetAttractable : MonoBehaviour {
 
     [SerializeField] private float magnetStrength;
     [SerializeField] private float magnetRange;
 
     private Rigidbody2D ownRigidbody;
     private Transform attractedTo;
 
     private void Awake () {
         ownRigidbody = GetComponent<Rigidbody2D>();
     }
 
     private void OnTriggerEnter2D (Collider2D other) {
         if (other.gameObject.tag == "PlayerShipTag") {
             attractedTo = other.transform;
         }
     }
 
     private void OnTriggerExit2D (Collider2D other) {
         if (other.gameObject.tag == "PlayerShipTag") {
             attractedTo = null;
         }
     }
 
     private void FixedUpdate () {
         if (attractedTo != null) {
             Vector3 direction = (attractedTo.position - transform.position).normalized;
             float forceFactor = 1 - direction / magnetRange; // magnetism is actually inversely proportional to the square of distance, but this is good enough approximation
              if (forceFactor > 0) {
                  ownRigidbody.AddForce(direction * magnetStrength, ForceMode2D.Force);
             }
         }
     }
 
 }
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
avatar image
0

Answer by diego-giacomelli · Aug 08, 2020 at 11:27 AM

@drpelz, I think you could try to use a PointEffector2D.

This video tutorial is a good start point: https://www.youtube.com/watch?v=znciJ1-QRX8

Comment
Add comment · Show 1 · 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 drpelz · Aug 08, 2020 at 01:40 PM 0
Share

Thank you so much for the hint!:)

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

474 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need Help Ending A Game - Alphabet Game 1 Answer

cannot implicitly convert type unityengine.vector3' to unityengine.transform' 1 Answer

Making a bubble level (not a game but work tool) 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