• 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 Gert-Willem · Nov 20, 2013 at 12:24 PM · c#animationparentchild

Rotate similar child objects around own origin

Hi

I have 1 parent object with 60 similar child objects. I want to rotate all those child objects around their own origin. I succeeded into rotating the entire parent object, but that's not what I wanted. How do i do this with a script or animation? I'm a beginner so I guess this probably won't be that difficult.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by eptanska · Nov 20, 2013 at 02:13 PM

The question is how you can identify the child objects you want to rotate. If you can simply add a script into each of your child objects, put the rotation code you got there. Note the difference between transform.rotation and transform.localRotation.

If you can only add the script to the parent, you have to loop through the (grand)children to find the ones you want to rotate, see this (more difficult). You can identify the right children by having a special name (prefix) for them.

However, I would recommend having a script in each of the children. If the children are exactly the same, make all the children to use the same prefab, and attach the script to the prefab itself. This way you can create new children easily when you need more (even during gameplay).

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 Gert-Willem · Nov 21, 2013 at 02:41 PM

First of all, thanks for the answer, but i'm still having some problems with this. I have added this script to my trigger. Isn't it possible to call all the child objects of the target, instead of the target itself.

 using UnityEngine;
 using System.Collections;
 
 public class rotateObjectX : MonoBehaviour {
 
     public GameObject target;
     public float angle = 90.0f;
     
     private float targetValue = 0.0f;
     private float currentValue = 0.0f;
     private float easing = 0.1f;
     
     void Update () {
         currentValue = currentValue + (targetValue - currentValue) * easing;
         target.transform.rotation = Quaternion.identity;
         target.transform.Rotate(currentValue, 0, 0);
     }
     
     void OnTriggerEnter (Collider other) {
         targetValue = angle;
         currentValue = 0.0f;
     }
     
     void OnTriggerExit (Collider other) {
         targetValue = 0.0f;
     }
 }
 
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 eptanska · Nov 22, 2013 at 12:59 PM 0
Share

If you want to rotate all direct children of that game object, you can do the following:

  void Update () {
     currentValue = $$anonymous$$athf.Lerp(currentValue, targetValue, Time.deltaTime * easing);

     foreach (Transform child in transform) {
         child.rotation = Quaternion.identity;
         child.Rotate(currentValue, 0, 0);
     }
  }

Note that you have to take in account the variable frame rate somehow (Time.deltaTime) and that there's a ready made easing function for you (with some variants).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Make a simple tree 1 Answer

Play parent animation without affecting the child animation 1 Answer

Respawn timer? Duplicate instance OnDestroy question... 0 Answers

Children won't animate 2 Answers

Child Parent problem 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