Locking world rotation of child

Hey guys,

I am trying to make an object rotate down when the player is running. It is supposed to rotate 25 degrees down and become unaffected by further camera movement meaning that when the player looks up while running the object still points towards the ground. When the player stops running it is supposed to align with the camera again. This means that the flashlight is still aligned with the players y rotation but not with his x.

The object itself is a child of the camera and the camera is a child of the player.

#pragma strict

var playerCamera : Transform;
var rotation : float;
var speed : float = 5;

private var startRotation : float;
private var startRotationWorld : float;

function Start(){
	startRotation = transform.localEulerAngles.x;
}

function Update (){
	print(playerCamera.eulerAngles.x);
	print(transform.eulerAngles.x);
	if(eventHandler.running){
		transform.localEulerAngles.x = Mathf.Lerp(transform.localEulerAngles.x, rotation, Time.deltaTime * speed);
		//THIS DOES NOT WORK transform.eulerAngles.x = Mathf.Lerp(transform.eulerAngles.x, 0, Time.deltaTime * speed);
	}
	
	else if(!eventHandler.running){
		transform.localEulerAngles.x = Mathf.Lerp(transform.localEulerAngles.x, startRotation, Time.deltaTime * speed);
		//THIS DOESN'T EITHER transform.eulerAngles.x = Mathf.Lerp(transform.eulerAngles.x, playerCamera.eulerAngles.x, Time.deltaTime * speed);
	}
}

Your problem is that you are setting localEulerAngles.x directly. This does not work well in unity instead create a new vector3 and set the angles like this

var tmp : float = Mathf.Lerp(transform.localEulerAngles.x, startRotation, Time.deltaTime * speed);
transform.localEulerAngles =  new Vector3(tmp, transform.localEulerAngles.y, transform.localEulerAngles.z)

This happens because localEulerAngles and all accessors return a copy of the property they are accessing thus modifying localEulerAngles.x is actually modifying the copy of it not the value you want.