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 slay · May 07, 2012 at 05:41 PM · rigidbodyarrayvelocitymultiple objects

Velocity of multiple rigidbody obiects in array

Hi, I'm trying input few rigidbody objects in array by tag, and then get the sum of velociy of all objects in array. But it's not working, error: "velocity is not member of Array." Here is code:

 var allObjects          :   Array;
 var allObjectsVelocity  :     float;
 
 function Start () {
     //input all tagged objects in array
     allObjects = GameObject.FindGameObjectsWithTag("Red") + GameObject.FindGameObjectsWithTag("Blue") + GameObject.FindGameObjectsWithTag("Green");
 }
 
 function Update () {
     //get valocity of multiple object
     allObjectsVelocity = allObjects.velocity.magnitude;
     print(allObjectsVelocity);
 }
 

Do you have any ideas? Thanks for help.

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

4 Replies

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

Answer by Adamcbrz · May 08, 2012 at 12:53 PM

you have 2 options one is. Store an array of rigidbodies or loop through game objects.

Option 1:

 var allObjects : Rigidbody;
 
 function Start()
 {
       allObjects = new Rigidbody[3];
       allOjects[0] = GameObject.FindGameObjectsWithTag("Red").rigidbody;
       allOjects[1] = GameObject.FindGameObjectsWithTag("Blue").rigidbody;
       allOjects[2] = GameObject.FindGameObjectsWithTag("Green").rigidbody;
 }
 
 function Update()
 {
      float allObjectsVelocity = 0;
      foreach(rigid : Rigidbody in allObjects)
      {
           allObjectsVelocity += rigid.velocity.magnitude;
      }
      print(allObjectsVelocity);
 }

Options 2:

 function Update()
 {
      float allObectsVelocity = 0;
      foreach(var go : GameObject in allObjects)
      {
           allObjectsVelocity += go.rigidbody.velocity.magnitude;
      }
      print(allObjectsVelocity);
 }
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 slay · May 08, 2012 at 05:02 PM 0
Share

THAN$$anonymous$$ YOU!!!! Second option works perfect:) For other beginners my code in clean javasript looks like:

function Update () { allObjectsVelocity = 0; for(var go : GameObject in allObjects) { allObjectsVelocity += go.rigidbody.velocity.magnitude; }

 print(allObjectsVelocity);

}

avatar image
0

Answer by Adamcbrz · May 07, 2012 at 05:49 PM

The issue is that the return type of allObjects is an Array you need to loop through all of the Array Items to sum the magnitude.

 function Update() {
     allObjectsVelocity = 0;
     foreach(rigid : Rigidbody in allObjects)
     {
         allObjectsVelocity += rigid.velocity.magnitude;
     }
 
     print(allObjectsVelocity);
 }

This is not tested and I usually use C# so syntax may not be perfect.

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 slay · May 07, 2012 at 06:28 PM 0
Share

Thanks for help, I'm trying now translate this on javascript. It's not so easy, I'm beginner on progra$$anonymous$$g:)

avatar image
0

Answer by slay · May 08, 2012 at 02:39 AM

ok, I'm trying but it's still something wrong. Now the code looks like:

 var allObjects            :   Array = new Array();
 var allObjectsVelocity    :   float;
 var i                   :   int;
 
 
 function Start () {
     //input all tagged objects in array
     allObjects = GameObject.FindGameObjectsWithTag("Red") + GameObject.FindGameObjectsWithTag("Blue") + GameObject.FindGameObjectsWithTag("Green");
 }
 
 function Update () {
     allObjectsVelocity = 0;
 
     for (i=0; i<allObjects.length; i++)
         {
         allObjects[i] = Rigidbody in allObjects;
         allObjectsVelocity += allObjects[i].velocity.magnitude;
         }
     
 
     print("test velocity : " +allObjectsVelocity);
 
 }

but now I have error : "velocity is not member of object"

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 slay · May 08, 2012 at 04:58 AM

Now I think is better and simpler, but problem is now "Cannot cast from source type to destination type". Do you have any ideas how fix that. Code :

 function Update () {
     
     allObjectsVelocity = 0;
     for (var rigid : Rigidbody in allObjects)
         {
         allObjectsVelocity += rigid.velocity.magnitude;
         }
     
 
     print(allObjectsVelocity);
 
 }

Thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Prevent rigidbody from colliding 1 Answer

Smooth Player Ball Rolling 1 Answer

How to change velocity of object which move by Rigidbody. 1 Answer

Unity limiting max velocity on Rigidbody? 5 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