- Home /
How do I make one transform rotation the same as anothers?
Right now I'm trying to take my cameras X rotation and make that my characters right forearm rotation. Can anyone help me with it? I keep getting errors when I try different things.
I can't simply do rightForearmRotation.z = cameraRotation.x; (Mind you, I'm using C#)
Try building a new Quaternion.
Quaternion rot = new Quaternion(rightForearmRotation.x, rightForearmRotation.y, cameraRotation.x, rightForearmRotation.w);
then
rightForearmRotation = rot;
this will literally make the z of the arm = the x of the camera HOWEVER it may not be result you are hoping it to be, because quaternions are evil.
Answer by Piflik · Jan 03, 2013 at 10:40 AM
If a component of a vector is read only (like transform.position), you can use a temporary vector, modify it and then replace the original vector with it.
Vector3 temp = transform.position;
temp.z = target.transform.position.x;
transform.position = temp;
But in this case, you can simply do this (methinks):
transform.rotation = Quaternion.Euler(transform.rotation.x, transfom.rotation.y, target.transform.rotation.x);
Since Quaternion.Euler already returns a new Quaternion.
Modifying the components of a Quaternion directly is a bad idea in any case, unless you know exactly how Quaternions work.
Neither work, when I try to make it a vector3 it tells me "Assets/Scripts/Player/Arm$$anonymous$$ovement.cs(14,31): error CS0236: A field initializer cannot reference the nonstatic field, method, or property"
As far as the quaternion option, I can't make heads or tails of it.
That should be...
transform.rotation = Quaternion.Euler(transform.rotation.x, transfom.rotation.y, target.transform.rotation.z);
(Note the final z
)
Answer by Maulik2208 · Jan 03, 2013 at 11:51 AM
public GameObject cube1;
public GameObject cube2;
// Use this for initialization
void Start ()
{
cube1.transform.rotation = cube2.transform.rotation;
}
For checking this script create a new scene and take 2 cubes......then rotate the 2nd cube as you wish......Drag this script to cube 1........assign both cubes to inspector panel.....Hit play and Done.....Cube is Rotated to Rotation of Cube 2......Enjoy.....If found useful then don't forget to mark the answer......
I like how you didn't read his question. He wants the z of his a arm to = the x of the camera. Not a whole rotation clone..
That was for a sample to Copy the whole Rotation.....For $$anonymous$$ore Specification he can do modification in my code....like say cube1.transform.rotation.z = cube2.transform.rotation.x; And it will work.....Then all he has to Do is Replace the Cube1 & Cube2 to his Gameobject......
But Thanks For you $$anonymous$$gestion i will keep it in $$anonymous$$d.... @$$anonymous$$aGuSware™
I have the same probles Piflik, your first worked for me, but as magusware sad, it isn't was he asket for, your second answer, "cube1.transform.rotation.z = cube2.transform.rotation.x" doesn't work, it sas it you can't modify transform.rotation. Do you have a solution for this?
For some reason it rotates a frame later than the second object for me.
Your answer
Follow this Question
Related Questions
transform.position on basis of rotation 0 Answers
Problem changing rotation of 3rd Person Controller 1 Answer
Align objects based on child objects 0 Answers