- Home /
Making a rotating object rotate with a parent object
I have a game object which swings back and forth along one of its axis, this is attached to a parent gameObject which moves around a scene. However, when the parent object rotates the child object does not rotate with it.
I expect this is something to do with local vs global space.. however Im at a loss...
This is the code which manages the sweep.
// Increment a timer
timer += Time.deltaTime;
// Swing the object back and forth based on the rotationLimit and the rotationSpeed
currentRotation = Mathf.PingPong (timer * rotationSpeed, rotationLimit * 2f) - rotationLimit;
// Create a temporary variable to store the new rotation
Quaternion rotation = Quaternion.Euler (transform.rotation.x, transform.rotation.y + currentRotation, transform.rotation.z);
// Set the rotation to the temp var
transform.rotation = rotation;
I considered grabbing the parents rotation and using that instead of the transform.rotation. However, ideally this same script needs to work on a variety of objects, some of which don't have parents. Can anyone advise where I am going wrong.
Answer by sevensixtytwo · Oct 27, 2014 at 12:49 AM
Try using transform.localRotation instead of transform.rotation.
transform.localRotation = rotation;
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
How to always rotate clockwise 1 Answer
Rotating Joystick on Touch 0 Answers
How to rotate about 360 on a coroutine 3 Answers
How to rotate a tank turret and gun? 2 Answers