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 Rotarus · Jun 26, 2012 at 05:29 PM · objectsmovemagnetgathering

Gather objects in one point

Hello! Is it possible to gather rigidbodies in one point, so they move to each other and meet in point, average for all of them. Just like magnets... I am trying different ways, but still can't get it.

Searching forums, I found a nice workable code for magnet-like effect, but it gather all objects around one (for which this code is applied). So it's like a "magnetic bomb". Not what I need exactly... Anyway, here is it:

 var blastForce = 0;
 var blastRadius = 10;
 var implode = true;
 
  function FixedUpdate () {
      var things : Collider[] = Physics.OverlapSphere(transform.position, blastRadius);
      for (i=0; i<things.length; i++){
          if (things[i].attachedRigidbody && things[i].attachedRigidbody != rigidbody){
              var offset : Vector3 = (transform.position - things[i].transform.position);
              things[i].attachedRigidbody.AddForce(offset/offset.magnitude * -blastForce);
          }
      }
  }
 
  function Update() {
      var key1 = Input.GetKey("1");
      var key2 = Input.GetKey("2");
      if (key1 && implode){
          implode = false;
          blastForce = -100;
      }
      if (key2 && !implode){
          implode = true; 
          blastForce = 50;
      }
  }

Thank you for answering!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by czebrose · Jun 26, 2012 at 06:17 PM

Couldn't you just average the positions of all the points and then apply a force on all the objects towards that point?

something like this:

 float blastForce = 5.0f;
 Vector3 total = Vector3.zero;
 for(thing in things)
 {
    total += thing.transform.position;
 }
 Vector3 center = total/things.length;
 for(thing in things)
 {
      thing.attachedRigidbody.AddForce((center - thing.transform.position)*blastForce);
 }
 
 

I think that'd do it.

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 Rotarus · Jun 26, 2012 at 08:24 PM 0
Share

czebrose, thank you for answering, but eeh... I am afraid that don't really understand how to use this code. I guess it should be a little bit changed for javascript?

I adapted it like this:

 var blastForce : float = 5.0f;
 var total : Vector3 = Vector3.zero;
 var blastRadius = 10;
 
  function Update() {
 var things : Collider[] = Physics.OverlapSphere(transform.position, blastRadius);
 for(thing in things)
 {
    total += thing.transform.position;
 }
 var center : Vector3 = total/things.length;
 for(thing in things)
 {
      thing.attachedRigidbody.AddForce((center - thing.transform.position)*blastForce);
 }
 }

but nothing works..

avatar image czebrose · Jun 26, 2012 at 09:02 PM 0
Share

I just tried this with a few boxes and it sort of worked. The boxes ended up falling to oblivion, so you should probably add a stop condition.

That aside, have you tried increasing the size of the blastRadius to say 1000? and do all of your objects have colliders and rigidbodies?

avatar image Rotarus · Jun 28, 2012 at 10:37 AM 0
Share

Sorry for answering so late! Yes, I tried that, after your post, but still nothing! $$anonymous$$aybe I missed something? $$anonymous$$aybe some special tags to these rigidbodies?..

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Move Objects in beginning of scene 1 Answer

Magnetic object to make coins move to object 2 Answers

Objects automatically move toward another object 1 Answer

Changes not reflected in play mode 1 Answer

Why do my objects keep changing speed? 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