- Home /
How make child object rotate inverse to its parents rotation ?
Hi guys , I thought that this would be easier done then said but I was wrong.
I have an object in my scene named ParentObject and it has a child object named Pivot
Pivot has a line render on it(just to help us visualize the rotation)
Picture:
we see that the parent and child rotation values are all 0, which is good. if we rotate our parent object, the child object will follow the parents rotation as expected but we do not want this.
Because the child rotation should always be inversely proportional to the parent rotation it will appear to be at a 0,0,0 rotation , which i what we want. So common sense would tell us that all we need to do is add or add or subtrack some rotational value from the child rotation to bring it back to what APPEARS to be a 0,0,0 rotation. but that does not work
Look at the now rotation values and the direction in which the Y axis is now in.
if we change the y value of the child object to compensate for the Y rotation of the parent , then line will tilt and everything goes wrong.
Answer by gangafinti · Aug 17, 2016 at 11:43 PM
This works fine for me:
using UnityEngine;
using System.Collections;
public class RotationTest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (0.0f, 1.0f, 0.0f);
transform.GetChild (0).Rotate (0.0f, -1.0f, 0.0f);
}
}
Try not to set euler angles manualy, always use Quaternion.Euler but in this case you can use the Rotation method that avoids the whole euler angles/Quaternion mess.
Yeah I was using Quaternion and Euler ... most confusing things ever.
But the Rotate Function is creating a constant rotation which is not good
sorry, that just gives a continuous rotation . i'm lost now
using UnityEngine;
using System.Collections;
public class RotationTest : $$anonymous$$onoBehaviour {
float angleToAdd = 45;
void Start () {
transform.rotation = Quaternion.Euler (transform.eulerAngles.x, transform.eulerAngles.y + angleToAdd , transform.eulerAngles.z);
transform.GetChild (0).rotation = Quaternion.Euler (transform.eulerAngles.x, transform.eulerAngles.y - angleToAdd , transform.eulerAngles.z);
}
}
This should do what you want.
Are you testing with a line render attached to your child object like in the example ?
I tested it with 2 cubes aka gameobjects and it worked. A line renderer should make no difference, because its the transform you are turning. The line will always face the camera maybe thats what you mean, becuase as far as I know you can't turn that.
Your answer
Follow this Question
Related Questions
Quaternion to Euler conversion singularities 0 Answers
Limiting the camera's rotation using quaternions, camera sticking to top/bottom limit angles 0 Answers
Flip over an object (smooth transition) 3 Answers
Particle System Instantiate's With Original Rotation - C# 1 Answer
Rotation with a slider 2 Answers