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.

Hi Moor, What do you wanna see in my screen?

–

2 Answers

2

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.

Hi aldonaletto, Thanks! What about y and z?

–

What exactly do you want to do? Rotate the plane to face the camera? Or rotate the plane with the keys? Or what?

–

MoveDirection is determined 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 mind 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.

–

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?

–

Hi, I understand now. Why tempRotation.x += rotateSpeed? I am changing the x based on the moveDirection, not rotateSpeed.

–