- Home /
Rotate object to be like target one Axis ?
I made a new scene to test rotation stuff in my game and now i have two cubes.
i want cube1 yRotation to be the same as cube2 yRotation i tried using euler angles but it's rotating in the world y axis or in the global y axis not the cube1 y axis.
Here is the script.
#pragma strict
var target : Transform;
function Update () {
transform.eulerAngles.y = target.eulerAngles.y;
}
I want cube1 to rotate as the target y axis in his local y axis not the world global y axis .
Thanks for any help.
I don't thing this is generally solvable. The problem is that there are multiple ways to get to a specific rotation, and there are multiple eulerAngle respresentations for any given rotation. For example (180,0,0) is the same physical rotation (0,180,180). So in taking an arbitrary rotation, we don't know what part of the rotation with creating by rotating on the 'y'.
You can solve it for specific situations, but for that we need more information.
How is the target rotated (script if you have it)?
Since you are only rotating on the 'y', I assume the x and z axes have a rotation? If so, can you describe it?
1- The target will only rotate on the y axis (still don't have a script).
2- The y will be like the target and the x and z axis will be like the slopes.
$$anonymous$$ay I add that you can snap an axis of an object to another axis, simply by going:
// Local Y Axis = Target's Local Y Axis
transform.up = Target.transform.up
But if you want more complex rotations, than I'm standing with robertbu, rotations are hells and you should be looking into Quaternions ins$$anonymous$$d of vector representation of the rotation
Hello , First of all 'm really sorry for saying this, I didnt get ur question but as much as I understand it I juat want to say that there is something : transform.rotation. eulerangles Or transform. rotation.localeulerangles
Find out something like this and all related scripts which contain this
I used this once in my game few years back
This might help you out.
Sorry If I gave u wrong answer. .
I'm still having trouble visualizing the situation, but if x and z are modest rotations, and target will only be rotating on the 'y', then this might work:
Vector3 v = target.forward;
v.y = 0.0;
transform.rotation = Quaternion.LookRotation(v);
Answer by Scribe · Aug 17, 2014 at 07:37 PM
The easiest way to do this is with a parent child hierachy setup. Have an empty gameobject with your cube1 as a child. On the cube1 have a script that copies the yRotation of cube2 and applies it to cube1's yRotation. then for x and z rotations, apply these to the empty parent object.
If for some reason you can't use a parent let me know and I will try and script the equivalent, I quite like the challenges that Quaternions throw up! :D
Scribe
EDIT:
Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, target.forward);
Quaternion up = Quaternion.FromToRotation(Vector3.up, transform.up);
transform.rotation = up*fwd;
this works 'somewhat' where it is attached to your cube1 and target is your cube2 though changing the other angles (x/z) appears to suffer some form of gimbal locking
Thanks for everyone who tried to help and thank you scribe because it worked fine,just what i wanted.
EDIT: why when i rotate the y axis 180 degrees it flips cube1.
Hmm... I'm unable to reproduce this problem, does it always happen for you all only at specific rotations of cube1?
It always happen when i rotate cube2 180 degrees on the y axis.
but i think that i found the solution ins$$anonymous$$d of using
Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, target.forward);
i tried using this
Quaternion fwd = Quaternion.FromToRotation(Vector3.right, target.right);
and it worked but i'm not sure if it's fully working.
strange, it never appears to happen for me. I expect it is because the Quaternion returned is the shortest change, so when it goes past 180 the quaternion changes to one rotating the opposite way around.
Some possible alternatives for Quaternion fwd that you could try:
Quaternion fwd = Quaternion.Euler(0, target.rotation.eulerAngles.y, 0);
Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, -Vector3.forward)*Quaternion.FromToRotation(-Vector3.forward, target.forward);
Quaternion fwd = target.rotation;
Your alternative looks like it should work, though I would have expected it to give the same reflection when 'right' passes 270degrees.
I still reckon that the parenting technique might be easier with less unreliable results, but it's up to you! :)
Scribe
Your answer
![](https://koobas.hobune.stream/wayback/20220613160803im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Issue with camera rotating on the Y axis 0 Answers
Rotate object with mouse 1 Answer
How to clamp Y rotation of camera? 1 Answer
Why is my Y rotation stuck at 180? 1 Answer