- Home /
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;
}
}
Your answer
Follow this Question
Related Questions
World and Local Axis out of Alignment 1 Answer
Apply a Rotation in World Space based on Quaternions 2 Answers
bone rotations changed in 4.3.2 inspector? 0 Answers
Why does the object alignment only work when the object isn't rotated? 2 Answers
an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers