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 DavidGracia · Mar 15, 2015 at 04:00 PM · rigidbody2dvolumemassweightdensity

Calculate objects' mass based on density and volume

Hello, I'm trying to calculate objects' masses based on density and volume.

Imagine I have a Box2DSprite and it is 1,1 (x,y), I could use mass=density*Box2D.localScale.x*Box2D.localScale.y and calculate it.

I want to order the boxes in a GameObject called Boxes and then they are parents. I want to calculate each box weight/mass individually based on the formula and then apply to the objects!

How could I do this?

Code in JS:

 #pragma strict
 
 var Density:float=1000; 
 
 function Start(){
 var Rigidbodies=gameObject.GetComponentsInChildren(Rigidbody2D);
 var Transforms=gameObject.GetComponentsInChildren(Transform);
 
 for (var Tfm : Transform in Transforms)
 for (var Rb : Rigidbody2D in Rigidbodies)
 Rb.mass=Density*Tfm.localScale.x*Tfm.localScale.y;
 }



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 AlwaysSunny · Mar 14, 2015 at 08:23 PM 0
Share

Please state the problem, and pay careful attention to how you phrase your posts. I do not understand the issue. Be very specific about what assistance you require. How to access the children on a transform? How to run your formula on each child and sum the results?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · Mar 15, 2015 at 04:55 PM

I think I see your problem. You're looping through each transform, and at each transform you are looping through every rigidbody and settting all rigidbodies with whatever mass you calculated for that rigidbody, so they will all end up with whatever you calculated for the last one.

Instead, try something like this (not good at javascript sorry for errors)

 //all child transforms
 var children = gameObject.GetComponentsInChildren(Transform);
 
 for (var child : Transform in children){
  float calculatedMass = child.localScale.x*child.localScale.y*density;
  //not sure you can get a rigidbody from transform like that. 
  //otherwise I guess child.gameObject.rigidbody is the way to access it?
  Rigidbody body = child.rigidbody;
  if(body) body.mass = calculatedMass;
  else Debug.LogError("This object has no rigidbody on it!");    
 }








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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How is Rigidbody 2D Auto Mass calculated? 2 Answers

How to give dynamically add mass to a gameobject from volume(collider.bounds.size) and density 1 Answer

Can I make mass and density constant independent of Collider 2D size? 1 Answer

Calculating rigidbody mass 1 Answer

Can't change Rigidbody2D mass 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