Copy Rotation in script C#
Hi, Unity Community. I would like to know if there is any kind of feature like Copy Rotation from another object? or if there is another way to Copy Rotation and an offset let's say
This is some values that would be seen on the inspector
GameObject Transform:
Rotation: X => 0 ; Y => 45; Z =>0;
SecondGameObject that would follow the rotation of the GameObject (In the second GameObject I need it with an offset )
Rotation: X => 0 ; Y => 45; Z =>0;
In the next Script I have the following code written:
void Update() {
CopyRotation();
}
// Copy Rotation from GameObject
// default(Vector3) like Vector3(0, 0 , 0)
public void CopyRotation(GameObject Target, Vector3 Offset = default(Vector3))
{
/*transform.rotation = Quaternion.Euler(
Target.transform.localRotation.eulerAngles.x,
Target.transform.localRotation.eulerAngles.y,
Target.transform.localRotation.eulerAngles.x
); */
// This is working if I need to copy the Rotation too.
// transform.rotation = Target.transform.localRotation;
Offset = Vector3(0 ,10, 0);
// I need to use the Y rotation starting with 10 degrees
// But let's say I need the Z axis rotation starting with 10 degrees
//Offset = Vector3(0 ,0, 10); // is it possible?
transform.rotation = Target.transform.localRotation + Offset;
}
so let's say
This is some values that would be seen on the inspector
GameObject Transform:
Rotation: X => 0 ; Y => 45; Z =>0;
SecondGameObject that would follow the rotation of the GameObject (In the second GameObject I need it with an offset )
Rotation: X => 0 ; Y => 55; Z =>0;
I am not sure if explained well what idea I want to achieve in this script.
Keep in mind this Script is not working
The next function works very nice, it is like a parent but without following the parent rotation.
// Copy Location from GameObject
public void CopyPosition(GameObject Target, Vector3 Offset = default(Vector3))
{
transform.position = Offset + Target.transform.localPosition;
}
Answer by brunocoimbra · Dec 13, 2016 at 02:52 AM
To achieve something like that with rotation:
public void CopyPosition(GameObject Target, Vector3 Offset = default(Vector3))
{
transform.position = Offset + Target.transform.localPosition;
}
You can do that:
public void CopyRotation(GameObject Target, Vector3 Offset = default(Vector3))
{
transform.rotation = Quaternion.Euler(Offset) * Target.transform.localRotation;
}
Quaternion.Euler transform the given euler angles (andles in X, Y, and Z) into a Quaternion rotation.
Also, to "add" 2 Quaternions you should use the "* (multiply)" operator.
@brunocoimbra For some reason the OFFSET value in the second object and the rotation values shown in the inspector are differents. Do you know the closest reason?
The copy rotation works perfectly
Your answer
Follow this Question
Related Questions
Searching a function to get the rotation of one Vector3 in relation to another 1 Answer
Why won't my model rotate up on X axis? 1 Answer
How to rotate a ship using the right analog stick? 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
Rotate GameObject with children around itself on mouse drag 1 Answer