'(UNITY 2D) How do I flip a sprite based on parent's rotation?
I have a thing set up, that makes the gun rotate in the direction of the cursor around the player and I want to make it so, when it's past or below a certain rotation.z value, it flips the gun's sprite. This is the code I have:
using UnityEngine;
public class Shooting : MonoBehaviour {
private Camera _mainCam;
private Vector3 _mousePos;
public SpriteRenderer gun;
public GameObject thisObj;
private void Start()
{
_mainCam = GameObject.Find("Main Camera").GetComponent<Camera>();
}
private void Update()
{
RotatePoint();
RotateGun();
}
void RotateGun()
{
if (thisObj.transform.localEulerAngles.z is < 270f or > 90f)
{
gun.flipY = true;
}
else if(thisObj.transform.localEulerAngles.z is > 270f or < 90f)
{
gun.flipY = false;
}
Debug.Log(thisObj.transform.localEulerAngles.z);
}
void RotatePoint()
{
_mousePos = _mainCam.ScreenToWorldPoint(Input.mousePosition);
Vector3 rotation = _mousePos - transform.position;
float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0,0,rotZ);
}
}
Any help is appreciated, I have no idea why it's not working... I've tried thisObj.transform.rotation.z too, without any success.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

