Im creating a 2D shooter in Unity, and i have the movement script and. the shooting script basically fully complete. But i realized when I’m flipping my character, my shot-points flip erratically, making it so my character is basically shooting 180 degrees from where hes aiming, but flipping back doesnt solve it. Ive been stuck on this for a while, knowing that its probably an extremely simple fix but i cant seem to put my finger on it. Any help is nice.
Heres my aiming code:
private void Update()
{
Vector3 gunPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (gunPos.x < transform.position.x)
{
transform.eulerAngles = new Vector3(transform.rotation.x, 180f, transform.rotation.z);
}
else
{
transform.eulerAngles = new Vector3(transform.rotation.x, 0f, transform.rotation.z);
}
Vector3 mousePos = Input.mousePosition;
Vector3 gunPosition = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x - gunPosition.x;
mousePos.y = mousePos.y - gunPosition.y;
float gunAngle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if (Camera.main.ScreenToWorldPoint(Input.mousePosition).x < transform.position.x)
{
transform.rotation = Quaternion.Euler(new Vector3(180f, 0f, -gunAngle));
}
else
{
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, gunAngle));
}
And heres my character flip code in my movement script:
if ((velocity.x > 0 && !facingRight) || (velocity.x < 0 && facingRight))
{
flip();
}
void flip()
{
facingRight = !facingRight;
Vector2 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;
}