- Home /
Child object is not rotating with parent
So I have an enemy that follows the player, with a child object that is initially aligned exactly with the parent, that I want to constantly oscillate back and forth in the parent's forward 180 degree arc.
I have it working, but its using world space no matter what I try. Here's how I'm doing it:
function Update(){
if(reverse){
transform.Rotate(-transform.up*Time.deltaTime*30.0, Space.Self);
if(transform.rotation.eulerAngles.y < 90.0)
reverse = false;
}
else{
transform.Rotate(transform.up*Time.deltaTime*30.0, Space.Self);
if(transform.rotation.eulerAngles.y > 270.0)
reverse = true;
}
}
I've tried a multitude of combinations, between changing transform.up to transform.parent.up, Space.World, etc., to no avail :(
Answer by Waz · Sep 11, 2011 at 08:52 PM
Sounds like you want to use localRotation, not rotation.
Ugh, such a noobish mistake! For some reason I kept thinking transform.rotation.eulerAngles would be giving me the local by default, like transform.forward/transform.right, etc... and I was wondering why when I was echoing the eulerAngles my values were completely different than the inspector!
So I got it working, had to change my values in the if statements because they are different for local. It works great, it's a particle system parented to an enemy shooting fire back and forth, and its oscillating properly and spraying fire back and forth no matter where the parent object rotates. However, after about 30 seconds of running the game it gets stuck occasionally. A gimbal lock issue, maybe?