how to get transform.eulerAngles

I am trying to mirror rotational movement of a camera onto another object. note: I can not simply make the object a child as the object happens to be a servo that’s run via Arduino. I have all the software running well… so for example I can move a slider within Unity and this moves the servo… but now I’d like to have the servo move in relation to main camera.

Overview: I have an Oculus Rift head mounted Display. When the user turns his head the servo should also turn. I have 2 servos. One for the X-axis & one for the y-axis.

Here’s the code that I am altering I need to change the function " void OnGUI() " which is currently a horizontal slider to the main camera’s transform.eulerAngles.x yes ??

public class Servo : MonoBehaviour {
		
		public Arduino arduino;
		public GameObject camera;
		
		public int servo_pin = 9;
		// Use this for initialization
		void Start () {
		
			arduino = Arduino.global;
			arduino.Log = (s) => Debug.Log("Arduino: " +s);
			
			arduino.Setup(()=> 
			{
				Debug.Log("set pin mode");
				arduino.pinMode(servo_pin, PinMode.SERVO);
			});
			
			
		}
				
		int servo_pos = 90;
		void OnGUI()
		{
			GUILayout.BeginArea(new Rect(300, 400, Screen.width/3, Screen.height-100));
			
			int new_servo_pos = (int)GUILayout.HorizontalSlider(servo_pos, 0, 180, GUILayout.Height(21), GUILayout.Width(150));
			
			if (new_servo_pos != servo_pos)
			{				
				arduino.analogWrite(servo_pin, (int)new_servo_pos);
				servo_pos = new_servo_pos;
			}
			
			GUILayout.EndArea();
			
		}
		
		// Update is called once per frame
		void Update () {
			transform.localEulerAngles = new Vector3(0,0,servo_pos);
			
		}
	}
}

SO my updated code looks something like this:

void Start () {
		
			arduino = Arduino.global;
			arduino.Log = (s) => Debug.Log("Arduino: " +s);
			
			arduino.Setup(()=> 
			{
				Debug.Log("set pin mode");
				arduino.pinMode(servo_pin, PinMode.SERVO);
			});
					
		}
				
		float servo_pos = 90;
		void MoveCam()
		{
			float new_servo_pos = this.transform.eulerAngles.x;
			
			if (new_servo_pos != servo_pos)
			{	
				Debug.Log("PLEASE PLEASE WORK !!!!");
				arduino.analogWrite(servo_pin, (int)new_servo_pos);
				servo_pos = new_servo_pos;
			}
			
		}
		
		// Update is called once per frame
		void Update () {
			transform.localEulerAngles = new Vector3(0,0,servo_pos);
			
		}  		
	}
}

So as you can see I am having difficulty changing a float to an int… & apart from that I get no error if I use only floats. The code runs with no errors but I can not see the DEBUG Print "PLEASE PLEASE " so I think perhaps I’m simply not calling the MoveCam(); function.

transform.rotation = Quaternion.Euler(0,0,0);

If this is your full code, then MoveCam is not called anywhere. OnGUI is called automatically (it is handled by Unity), but you have to call it inside Update, before changing transform.rotation:

void Update()
{
    MoveCam();
    transform.localEulerAngles = new Vector3(servo_pos, 0, 0);
}

Please note that I changed your Vector3 constructor call, because x is first value, not last.

EDIT: heh - I reread the question and just spotted the part related to not calling MoveCam… So I guess this must solve your problem :slight_smile: