- Home /
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.
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).
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;
}
}
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).