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 Tohron · Nov 22, 2013 at 03:41 AM · rigidbodygetcomponentloopjavascript-specific

Loop through Children Unable to Access Component Script

In the space shooter I'm designing, a dead ship spawns a bunch of ship fragments, which are supposed to drift apart and then disappear after a short time. To do that for my Slicer fighter, I have two scripts - SlicerFragElement, which is assigned to each of the child fragments, has velocity and angular velocity fields, and simply handles translating the frag it is assigned to. To set this up, I have the SlicerFragElement script, which is supposed to assign velocities to all of the individual frags. To do that, I have the following code (print statements are for debugging):

 var startTime : float;
 var duration : float = 5.0;
 
 function Start () {
     startTime = Time.time;
     //*
     for (var child : Transform in this.transform) {
         var distanceVec : Vector3 = child.position - transform.position;
         //print(distanceVec);
         //print(child.name);
         child.GetComponent("SlicerFragElement").velocity = 
                     Random.Range(5.0, 14.0) * (1 / Mathf.Pow(distanceVec.magnitude + .2, 2)) * distanceVec.normalized;
         //print(child.GetComponent("FalconFragElement").angularVel);
     }
     
 }
 
 function Update () {
     if (Time.time > startTime + duration) {
         Destroy(this.gameObject);
     }
 }

The problem is, this code cannot seem to modify the SlicerFragElement scripts for any of the children. Their velocity stays at 0, the print statements show (0,0,0,0) and (0,0,0) for the angularVel and velocity, even when the angularVel (assigned in SlicerFragElement ) is nonzero. However, the names do show up correctly.

Any idea what the problem is? Also, would just assigning rigidbodies to each of the frag elements be more processor-efficient (there may be hundreds of these onscreen at once, so that's a concern)?

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

Answer by Programmer-Tau · Nov 22, 2013 at 05:38 AM

Without the code for SlicerFragElement, it's kind of hard to say.

My first suggestion is to simply set the velocity to some constant value, if you haven't tried that already. If it's still always zero, then I would suspect something in the SlicerFragElement code that is resetting it to zero.

At a much higher level though, you may want to use Unity's particle system (Shuriken) for something like this anyway. If there's no gameplay element to the fragments, then using Unity's particle system will be much more efficient, and is less work in the long run.

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 iwaldrop · Nov 22, 2013 at 07:36 AM 0
Share

Particles FTW. Also, they're probably going to be cheaper than a bunch of geometry, unless your geo is less than 4 verts.

avatar image Tohron · Nov 22, 2013 at 03:40 PM 0
Share

So how would you use particles for this? Bear in $$anonymous$$d that the fragments were obtained by manually splitting apart the fighter mesh in Blender, so each fragment is unique.

Also, here's the code for SlicerFragElement: var velocity : Vector3 = Vector3.zero; var angularVel : Quaternion;

 function Start () {
     angularVel = Random.rotation;
 }
 
 function Update () {
     transform.Translate(velocity * Time.deltaTime, Space.World);
     var rotTarget = transform.rotation * angularVel;
     transform.rotation = Quaternion.RotateTowards(transform.rotation, rotTarget, 20 * Time.deltaTime);
 }

It's pretty simple, which is why I didn't post it earlier.

avatar image Programmer-Tau · Nov 22, 2013 at 05:08 PM 0
Share

Hmm, yeah, it looks pretty straight forward, and I don't see anything that would keep velocity and rotation at zero. I assume you've checked this, but are you sure Start and Update are being called on SlicerFragElement? Sorry I wasn't much help here.

Regarding the use of particles, if you want to be physically accurate and use fragments you created from Blender, then your approach is the right way. If you use particles, you do need to limit the kind of geometry you spawn. With that said, if you're spawning hundreds in the screen, you'll be spending a lot of draw calls and CPU simulation time on what sounds like a cosmetic effect.

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

18 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

Related Questions

Disable all child rigidbodies 1 Answer

Destroy dont work on Rigidbody2D 1 Answer

GetCompent() adds a Rigidbody 2 Answers

OnTriggerStay, add the mass of all objects inside the trigger together 1 Answer

Push Rigidbody ball around a loop de loop? 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