2D: Rotating to face mouse position with two fixed axes

I’m working on a 2D sidescrolling platformer with keyboard movement and mouse aiming controls. The character’s arms should point in the direction of the mouse cursor on the screen, but rotate independently from the body of the character. Additionally, I’d like to have an easy reference to the rotational position of the arms so that I might apply forces in that direction.

Making the arms children of the character object, I can get them to point to the mouse with the script below, but I run into one problem. Whenever the mouse crosses the vertical centre of the screen, the y-rotation of the arms flips, essentially reversing the position of the near and far arms of the character. This also has the side effect of altering the x-rotation, making it so I can’t count on anything related to it to act as expected in world space.

Is there a better way to do this?

function Update (){
var mousePos = Input.mousePosition;
mousePos.z = 10.0f; //The distance from the camera to the player object
var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
transform.LookAt(lookPos);
}

I think this is what you are looking for, or at least a start. It is different for the code above in that it aligns the right of the object (as opposed to the forward) with the mouse. If your object is not authored that way, you might be able to fix it by making the arms children of an empty game object and attaching the script to the empty game object.

As for adding force in the direction of the arms, depending on your frame of reference it will be either transform.right or -transform.right.

function Update () {
	var mousePos = Input.mousePosition;
	mousePos.z = 10.0f; //The distance from the camera to the player object
	var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
	lookPos = lookPos - transform.position;
	var angle : float = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
	transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}