# Rotation problem

I have an object that needs to be rotated in one axis but keeping the original rotation in the others.

Here is the code I wrote for doing that:

``````using UnityEngine;
using System.Collections;

public class rotation : MonoBehaviour {

// Elements
Transform elevator;

// Min/Max angles
public float elevatorAngle;

// Use this for initialization
void Start ()
{
elevator = GameObject.Find("C_elevator").transform;
}

// Update is called once per frame
void Update ()
{
float vert = Input.GetAxis("Vertical");

elevator.localEulerAngles = new Vector3(elevator.localRotation.eulerAngles.x - (elevatorAngle * vert), elevator.localRotation.eulerAngles.y, elevator.localRotation.eulerAngles.z);
}
}
``````

The problem is that it’s not working properly. Please take a look this video to see the problem: http://www.youtube.com/watch?v=ewKVm95o364

Euler angles can change representation on you. For example put this in a script:

``````transform.eulerAngles = new Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
``````

This will output (0,180,180), which is the same physical rotation, but a different Euler angle representation.

A way to fix this problem is to maintain your own rotation and assign it. You never read transform.eulerAngles or transform.localEulerAngles. Consider them write-only. So you create a variable called elevatorXRotation. You could do:

``````elevatorXRotation -= (elevatorAngle * vert);
elevator.localEulerAngles = new Vector3(elevatorXRotation, 0.0f, 0.0f);
``````

I’ve fixed it
At the end has been really simple I just have to save the initial rotation of the object in a Vector 3 and then apply the rotations keeping the initial rotations in the other axis:

``````//Initial rotaion
Vector3 alieronLRotation;

alieronL.localEulerAngles = new Vector3((alieronAngle * hori + alieronLRotation.x), alieronLRotation.y, alieronLRotation.z);
``````