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 EncryptedCow · Apr 25, 2013 at 07:46 PM · javascriptchildpositioning

placing children objects in different places

I'm making a script where the player can chop down trees with an axe and it's going pretty good so far. However I've hit a small obstacle now. At a certain point, I want to respawn the trees (multiple objects per tree) in the same spot they were at. However I can't seem to figure out how to give each object a unique position. I've got my tree prefab and I want to set up the tree exactly how that is. Right now I just check each child object and put it back at a certain position but obviously this is going to be the position for each object. Is there a better way or similar way that works to do this? Heres the code I'm using right now.

 var treeDead : boolean = false;
 var treeHealth : int = 5;
 var logWorth : int = 1;
 
 function Update() {
     if(treeHealth <= 0) {
         if(!treeDead) {
             treeHealth = 0;
             TreeRespawn();
         }
     }
 }

 function OnTriggerEnter(itemHit : Collider) {
     if(!treeDead) {
         if(itemHit.tag == "Axe") {
             treeHealth -= 1;
         }
         if(itemHit.tag == "Chainsaw") {
             if(treeHealth >= 5) {
                 treeHealth -= 5;
             }
             else {
                 treeHealth -= treeHealth;
             }
         }
         if(itemHit.tag == "Big Chainsaw") {
             if(treeHealth >= 10) {
                 treeHealth -= 10;
             }
             else {
                 treeHealth -= treeHealth;
             }
         }
     }
 }
 
 function TreeRespawn() {
     for(var child : Transform in transform) {
         child.rigidbody.isKinematic = false;
     }
     treeDead = true;
     yield WaitForSeconds(50);
     for(var child : Transform in transform) {
         child.localPosition = Vector3(0, 2.75, 0);
     }
     for(var child : Transform in transform) {
         child.rigidbody.isKinematic = true;
     } 
     treeDead = false;
 }

The lines in question are 44 and 45 [for(var child : Transform in transform) { child.localPosition = Vector3(0, 2.75, 0); }] Which is obviously just setting each objects position to that.

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
Best Answer

Answer by TeddyDief · Apr 25, 2013 at 08:58 PM

You could store a List or array of localPositions, one for each child, in your Start() function. Then, when respawning your tree, do something like...

 var transforms : Transform[]; // Array of all Transforms
 var transformPositions : Vector3[];  // Array of all positions
 var transformRotations : Quaternion[];  // Array of all rotations
 
 // ... after your WaitForSeconds() in TreeRespawn
 
 for (var i = 0; i < transforms.Length; i++) 
 {
      transforms[i].rigidbody.isKinematic = true;
      transforms[i].localPosition = transformPositions[i];
      transforms[i].localRotation = transformRotations[i];
 }
 treeDead = false;
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 EncryptedCow · Apr 25, 2013 at 10:32 PM 0
Share

Aha! Thanks a whole bunch, that worked perfectly :)

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

12 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

Related Questions

Find children by tag from Player 1 Answer

How do I find out where a child is with javascript 3 Answers

Create multiple instances of an object 2 Answers

Create array of children in Start function, use it in Update? 1 Answer

Make a simple tree 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