How can I make character arm rotation/mouse aim work correctly?

[31462-2d+issues.jpg|31462]

Here is a bit of code. To use:

  • Setup your sprite so that the front of the sprite is facing right. Given your code, right now you have it facing up.
  • Attach this script
  • Disable whatever logic is currently flipping the the texture

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
	private float x;
	private Vector3 ls;

	void Start() {
		x = transform.localScale.x;
		ls = transform.localScale;
	}
	
	void Update () {
		Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position);  

		float rotZ = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;

		if (dir.x >= 0) {
			transform.rotation = Quaternion.Euler  (0f, 0f, rotZ);

			ls.x = x;
			transform.localScale = ls;
		}
		else {
			transform.rotation = Quaternion.Euler  (0f, 0f, rotZ+180);
			ls.x = -x;
			transform.localScale = ls;
		}
	}
}