- Home /
Question by
schindoneer5 · Jul 31, 2021 at 04:28 PM ·
2d game2d-platformer2d spritesflip2d-sidescroller
How do i Flip a 2d game object that is facing towards the cursor
I want to Flip a game object so that it looks good while still facing the cursor but with my (Shitty) code, when it is flipped, it only rotates about 45-90º Can anyone help me with this?
using UnityEngine;
public class ShoulderRotateFlip : MonoBehaviour
{
bool facingRight = true;
void Update()
{
// using mousePosition and player's transform (on orthographic camera view)
var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
if (delta.x >= 0 && !facingRight)
{ // mouse is on right side of player
transform.localScale = new Vector3(1, 1, 1); // or activate look right some other way
facingRight = true;
}
else if (delta.x < 0 && facingRight)
{ // mouse is on left side
transform.localScale = new Vector3(-1, 1, 1); // activate looking left
facingRight = false;
}
if (delta.x >= 0 && facingRight)
{
var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
else
{
var dir = Input.mousePosition + Camera.main.WorldToScreenPoint(transform.position);
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, -Vector3.forward);
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
Comment