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);
}