• 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 gabrielstuff · Aug 27, 2013 at 09:23 PM · rotationlocalworldtranslationtween

Rotation and Translation children parent and tween

Hi !

I have a group of GameObject which belong to an empty GameObject. This emtpy GameObject rotate with this simple function :

 transform.RotateAround (_bounds.center, Vector3.up, 20 * Time.deltaTime);

The _bounds variable is private and calculate dynamically depending on the geometrical movement of the children. Thus the rotation is always centered.

The problem comes when I add tween to my children. Indeed each children move independently from left to right, up and down, back and forth.

 HOTween.To(transform.GetChild(i), 10, new TweenParms()
                         .Prop("position", new Vector3(transform.GetChild(i).position.x + Random.Range(-5.0F, 5.0F), transform.GetChild(i).position.y + Random.Range(-5.0F, 5.0F), transform.GetChild(i).position.z + Random.Range(-5.0F, 5.0F)), false) // Position tween (set as relative)
                         .Loops(-1, LoopType.Yoyo) // Infinite yoyo loops
                         .Ease(EaseType.EaseInOutQuad) // Ease
                         .OnUpdate( OnTweenUpdate )
                         .OnStepComplete(moveComplete, transform.GetChild(i).name) // OnComplete callback
                     );


The tween are instantiate at start. Bellow you will find full code. The issue arise when both instruction run simultaneously. The tween update the position and the update update the rotation of parent which update the position of of children.

What I'd like is rotate AND move for each of my single children.

Any help would be much appreciate.

 using UnityEngine;
 using Holoville.HOTween;
 using System.Collections;
 using System.Collections.Generic;
 using Vectrosity; 
 
 
 public class GenerateLines : MonoBehaviour {
     private List<VectorLine> connectionList;
     private int childrenNumber;
     private Bounds _bounds;
         
     [Range(0.0f, 360)]
     public float rotationY;
     
     public Material lineMaterial;
     [Range(0.1f, 10.0f)]
     public float stroke;
     
     //private Material materialLine;
     
     void Start() {
         // First find a center for your bounds.
         calculateCenter();
         connectionList = new List<VectorLine>();
         HOTween.Init(true, true, true);
         
         childrenNumber = this.transform.childCount;
         var linePoints = new Vector3[2];    
         if(childrenNumber >= 2){
             for(int i = 0; i < childrenNumber; i++){
                         linePoints[0] = transform.GetChild(i).position;
                         linePoints[0].z +=  0.01f;
                         var lineName = gameObject.name + "-line-" + i;
                         if(i < childrenNumber - 1){
                             linePoints[1] = transform.GetChild(i+1).position;
                         } else {
                             linePoints[1] = transform.GetChild(0).position;
                         }
                         linePoints[1].z +=  0.01f;
                         connectionList.Add(new VectorLine(lineName, linePoints, lineMaterial, stroke));
                         connectionList[i].Draw3D();
                         
                     
                     HOTween.To(transform.GetChild(i), 10, new TweenParms()
                         .Prop("position", new Vector3(transform.GetChild(i).position.x + Random.Range(-5.0F, 5.0F), transform.GetChild(i).position.y + Random.Range(-5.0F, 5.0F), transform.GetChild(i).position.z + Random.Range(-5.0F, 5.0F)), false) // Position tween (set as relative)
                         .Loops(-1, LoopType.Yoyo) // Infinite yoyo loops
                         .Ease(EaseType.EaseInOutQuad) // Ease
                         .OnUpdate( OnTweenUpdate )
                         .OnStepComplete(moveComplete, transform.GetChild(i).name) // OnComplete callback
                     );
             }        
         }
     }
     
     void OnTweenUpdate(){
         Debug.Log("updateTween");
         calculateCenter();
         
         transform.RotateAround (_bounds.center, Vector3.up, 20 * Time.deltaTime);
     }
     
     void Update() {
         childrenNumber = this.transform.childCount;
         if(childrenNumber >= 2){
             if(connectionList.Count < childrenNumber){
                 Debug.Log(connectionList.Count);
             } else {
                 for(int i = 0; i < connectionList.Count; i++){
                         connectionList[i].points3[0] = transform.GetChild(i).position;
                         if(i < childrenNumber - 1){
                             connectionList[i].points3[1] = transform.GetChild(i+1).position;
                         } else {
                             connectionList[i].points3[1] = transform.GetChild(0).position;
                         }
                         connectionList[i].points3[0].z += 0.2f; 
                         connectionList[i].points3[1].z += 0.2f; 
                         connectionList[i].lineWidth = stroke;
                         connectionList[i].Draw3D();
                 }
             }
         }
     }
     
     void calculateCenter(){
         Vector3 runtimeCenter = Vector3.zero;
         
         foreach (Transform child in transform)
         {
             runtimeCenter += child.gameObject.renderer.bounds.center;   
         }
         runtimeCenter /= transform.childCount; //center is average center of children
 
         if(_bounds != null){
             _bounds.center = runtimeCenter;
             _bounds.size = Vector3.zero; 
         } else {
             _bounds = new Bounds(runtimeCenter,Vector3.zero); 
         }
         
         foreach (Transform child in transform)
         {
             _bounds.Encapsulate(child.gameObject.renderer.bounds);   
         }
     }
     
     void moveComplete(TweenEvent data){
         if ( data.parms != null ) {
             Debug.Log( data.parms[0] ); // outputs 1
           }
         Debug.Log("complete");
     }
     
     void OnMouseDrag() {
         renderer.material.color -= Color.white * Time.deltaTime;
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

15 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

Related Questions

an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers

simple rotation question c# local/world 2 Answers

Move object on local axis instead of world axis after rotation 0 Answers

Move object relative to its axis 2 Answers

Mesh won't travel in direction it's facing 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges