public Transform controlThisObject; //Here can be the transform of the weapon
private Vector3 mousePos;
public Quaternion angles;
public bool facingRight = true;
void Update()
{
MouseL();
angles = transform.rotation;
}
void MouseL()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 gunPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - gunPos.x;
mousePos.y = mousePos.y - gunPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
if (transform.rotation.w <=0.7 && !facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
else if (transform.rotation.w >= 0.7 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}