Rotate an object respectively to other object's rotation on z-axis
Say, we have obj1 and obj2.
I want rotation of obj2 around it's transform.forward to EQUAL the obj1.transform.rotation.z. HOW should i do it?
Note: obj2.transform.rotation.z = obj1.transform.rotation.z;
doesnt work fine if obj2 is already rotated around its x, y and z axis.
oh.. this isnt what I was thinking. wouldnt this just be solved by: obj2.transform.localRotation.z = obj1.transform.localRotation.z;
Cuddle$$anonymous$$onster, none of the objects is parented so z = z wont work (and i guess it either wouldnt work fine if the child obj had been initially rotated).
That answer was assu$$anonymous$$g that they weren't connected. localRotation should fix it unless there is more details you aren't sharing.
Ah, yes, `localRotation` is a Quaternion, and the x,y,z are almost random numbers -- not angles at all.
But, the docs still say that changing a single EulerAngle (just z) will give unpredictable results.
Answer by Nordir · Nov 07, 2012 at 09:28 PM
I figured it out. It's damn simple :)
obj2.transform.localEulerAngles.z = obj1.transform.localEulerAngles.z;
Doesnt work, you need to store it, so ins$$anonymous$$d I used the whole rotation to skip storing the value: transform.localEulerAngles = new Vector3(0.0f,$$anonymous$$yCamera.transform.localEulerAngles.y, 0.0f); (CSHARP)
This is in javascript. which allows step-through changes. It was the recommended Unity language back in 2012. With C# you'd need to break it out: Quaternion q=obj2.trans.lea; q.z= ... obj1.trans.lea=q;
I hate these differences and I hate the people that invent 1000 languages so we chase after our tails. I will also write in what language my method works, will edit my upper comment. Thanks.
Answer by Owen-Reynolds · Nov 06, 2012 at 10:14 PM
The manual says that reading out a single eulerAngle isn't reliable (even if it did sometimes work.)
So they can both face any direction, but the "roll" should be as equal as possible? Could abuse LookAt to copy #1's UP into #2. Will "flip" when obj2 is facing opposite obj1, but might still look nice:
Transform tr2 = obj2.transform;
Vector3 justAhead = tr2.position + tr2.forward;
obj12.transform.LookAt( justAhead, obj2.transform.up );
The LookAt won't change the way you face, but will "roll" obj2 to try to align its up with obj1's up. Alternately, if you're already using a lookAt, toss in the alternate up there.
Thanks Owen, but i need rotations to be precisely equal.
If in Unity editor i click "Local" button and rotate an obj around its Vector3.forward, i see that only its z rotation is changed. I cant get how to change rotation.z without changing x and y in script.
Cuddle$$anonymous$$onster, localRotation is the same thing as rotation if an obj is not parented:
http://docs.unity3d.com/Documentation/ScriptReference/Transform-localRotation.html
Your answer
Follow this Question
Related Questions
Move an object by rotation 0 Answers
Rotation is starting at 0, how do I use Math.Clamp() on it? 1 Answer
How i can set Start Position and Ending position in ROTATION AROUND AXIS? 0 Answers
Problems with rotating the player on the Y axis C# 0 Answers
Quaternion.LookAt() without restricting manual rotation 2 Answers