- Home /
Rotate a plane at x axis while fixing its y and z initial rotation value
Hi,
I have a plane that is already fixed at rotation (270, 0, 0) in Unity scene. What I wanna do now is to rotate the the plane around x axis, while mainting y and z at 0. When rotating, the direction to face is Vector3 moveDirection (for example).
Secondly, how to change the value of x or y or z rotation value and fix them when necessary?
For example,
If moveDirection = right,
Rotation (x, -630, -1530); //x is a changing value based on moveDirection
if moveDirection = left,
Rotation (x, -1170, 990);
For your information, I get the values above by rotating my plane.
I searched around ways to do it but all of them do not have fixing a rotation value while rotating. Please help.
Thanks in advance.
Answer by aldonaletto · Nov 21, 2013 at 02:13 AM
This is a good case for eulerAngles: save the initial eulerAngles in a member variable and "rotate" mathematically the angles, then assign the variable to eulerAngles:
public var rotationSpeed: float = 60; // 60 degrees per second
private var euler: Vector3;
function Start(){
// get the initial eulerAngles:
euler = transform.eulerAngles;
}
function Update(){
// rotate mathematically about X:
euler.x = (euler.x + rotationSpeed * Time.deltaTime) % 360;
// update object rotation:
transform.eulerAngles = euler;
}
You can modify the other angles at any time - but do it in the euler variable, not directly in transform.eulerAngles: any change in the euler variable will be transferred to the object inside Update.
What exactly do you want to do? Rotate the plane to face the camera? Or rotate the plane with the keys? Or what?
$$anonymous$$oveDirection is deter$$anonymous$$ed by dragging. I want to rotate the plane to face the endPosition of the drag. However, when I tried to rotate the plane manually in Unity editor, when rotate to left, x varies according to the rotation, y and z changes to another constant (x, -90, 90). When rotate to right, x varies and y and z changes to another constant (x, 90, -90). This is what unity shows.
Don't $$anonymous$$d about what appears in the Inspector's Transform/Rotation field: it's actually transform.eulerAngles, a completely "virtual" property that's calculated on the fly based on transform.rotation. Since many different XYZ combinations result in the same rotation, when converting a rotation to Euler angles Unity chooses one of the possible combinations - frequently an unexpected one. If the plane is rotating as expected, forget about the Rotation field.
Answer by tWayfarer · Nov 20, 2013 at 04:41 AM
If you want to only rotate a single axis, you can do something like:
tempRotation = currentRotation;
tempRotation.x += rotateSpeed;
currentRotation = tempRotation;
This way, the y or z axis won't change, but x will.
Thanks for the info! But right now, depending on my moveDirection, sometimes y and z will change. Like what I have written in my question. If moveDirection is right, the y and z change and remains a constant until moveDirection is left.
What I've written won't affect y or z, even if you change them elsewhere. What it does is get the current rotation, then just change the x, and give it back. Y and z are unaffected by it completely. Then, afterwards (or even before) you can change the y and z as you need to.
How to change the y and z as I need to? currentRotation will be used in which function?
This is more pseudocode than actual code. You'll want something like: Quaternion tempRotation = transform.rotation;
tempRotation.x += rotateSpeed;
if(moveDirection == right)
{
tempRotation.y = -630;
tempRotation.z = -1530;
}
if(moveDirection == left)
{
tempRotation.y = -1170;
tempRotation.z = -990;
}
transform.rotation = tempRotation;
It might not work if you copy/paste, but should be close. Unity always recommends NOT just changing x, y, z variables when rotating, because they have functions for it, but I don't know what they are because I don't work with rotation much. It's your best bet to combine this idea with some research into those functions.
Hi, I understand now. Why tempRotation.x += rotateSpeed? I am changing the x based on the moveDirection, not rotateSpeed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122252im_/https://answers.unity.com/themes/thub/images/avi.jpg)