- Home /
How to parent an object without losing its rotation
Hello!
I'm having a very severe headache over this problem. I'm setting an objects rotation using transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y + 360,transform.eulerAngles.z);
But when I try to make the object that this code is attached to a Child of an object that is rotating in a circle then the Object that this code is attached to loses its rotation. This problem seems to only be occurring when the parent's rotation is something other than 0. Is there a fix to this problem? It's causing a major headache because I can't find the solution to this.
GameObject + Correct Rotation = Correct Rotation GameObject + non-rotating Parent + Correct Rotation = Correct Rotation GameObject + Rotating Parent + Correct Rotation = Incorrect Rotation
The GameObject is whose rotation I'm setting after I parent it. Thank you for the help!
Well if you are parenting it, it will also assume it's parent's position and rotation - but you are overwriting it (every Update?) so it's a bit confusing.
No. I'm just calling the rotation once, AFTER it attaches to the parent. and it only attaches to the parent on start.
BTW the code you posted appears to apply no additional rotation (+360 on the Y axis).
So are you saying it isn't following the rotation of the parent - or it is and that's not what you wanted?
Well what's really going on is once the object is assigned a parent then the object takes the parent's rotation. How would I make it to where that child never takes the parents rotation, and sticks to only one rotation? That's what I'm having trouble with.
Answer by robertbu · Feb 22, 2014 at 04:16 AM
One solution in situations like this is to not parent. Instead have the 'child' follow what would be the parent.
var targetToFollow : Transform;
function LateUpdate() {
transform.position = targetToFollow.position;
}
Now the pseudo-child follows the parent but does not rotate with the parent. If you need an offset, you can either specify it in code, or you can have the pseudo-child calculate it from the relative positions of the two object in Start().
Agreed with Robert. You other choice is to capture the rotation and apply it every frame while still parenting the object:
var myRotation : Quaternion;
function Start() {
myRotation = transform.rotation;
}
function LateUpdate() {
transform.rotation = myRotation;
}
This causes myRotation to be the actual rotation of the item.
No these are very un-useful. I don't want the child to follow the parent by any means. I want it to rotate with the parent. All the while doing movements of it's own. Sort of like an orbit, but I have many of these objects that must move in uniform with eachother which is why they all have their own rotations.
Right in which case you need RotateAround and don't parent it - use that to move it.
And really - it's not particularly reasonable to downvote an answer because we couldn't understand the question. You'll find people are unlikely to want to help you under those circumstances.
Again whydoit, that's unhelpful. I'm particularly experience with the engine and I've tried pretty much everything I could. Which is why I came here for an answer. I tried using RotateAround, but it didn't help at all because there are 8 of these objects that all need to be uniform meaning that they must all be the same distance apart and everything.
Your answer
