Flipping shooting point

Hello,

I am making 2D shooter platformer game and I have my player flipping to mouse direction and weapon following mouse cursor. However, the shootpoint will not flip too and I dont know how to flip anything withou sprite :smile:. Can anyone help me with this one?

Thank you

How are you currently flipping your character?

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;
}
THIS IS IN MY PLAYERCONTROLLER SCRIPT

AND THIS IS IN MY SHOOT SCRIPT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShootScript : MonoBehaviour
{

public float offset;

private GameObject player;
private SpriteRenderer spriteren;
public GameObject Bullet;
public Transform ShootPoint;
public Animator anim;

public float BulletSpeed;
public float FireRate;
float readyForNextShoot;

private void Start()
{
player = GameObject.FindGameObjectWithTag(“Player”);
spriteren = GetComponent();
}

void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

if(rotZ < 89 && rotZ > -89)
{
spriteren.flipY = false;
spriteren.flipX = false;
}
else
{
spriteren.flipY = true;
spriteren.flipX = true;
}

if (Input.GetButtonDown(“Fire1”))
{
if(Time.time > readyForNextShoot)
{
readyForNextShoot = Time.time + 1 / FireRate;
shoot();
}
}
}

void shoot()
{
GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, transform.rotation);
BulletIns.GetComponent().AddForce(BulletIns.transform.right * BulletSpeed);
anim.SetTrigger(“Shoot”);
}

}

6564148--743890--upload_2020-11-26_23-22-4.png
6564148--743893--upload_2020-11-26_23-22-33.png

Make sure when posting code to use the code tags so it is formatted correctly.

Both scripts are essential doing the same thing. I would have it control the arm in the player script along with the flipping(so you are only calculating once) and have a gun or weapon script that controls the firing/attacking. One caveat is that when it’s flipped left with -1 scale it will invert the rotation, here is a snippet from a game I’m working on.

float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
if (Flipped)
    angle += 180f;

armPivot.rotation = Quaternion.Euler(0f, 0f, angle);

I am not flipping any spriterenderer’s, setting the scale.x to -1 already does that for me. I have no offset, I have the arm and the sprites facing right by default.

Your way doesnt flip the gun when it cross 90 or -90 angle and I still have to flip char. Do you have any advice how to solve this my little problem please? Thank you

It might be how you have it setup.

This is how I setup my player:

  • Player Body Sprite - Has the Player script that controls the arm pivot rotation
  • Arm Pivot(Shoulder) - Empty game object that is what get rotated
  • Arm Sprite - Only the arm sprite
  • Gun Attach Point - Empty game object allows swapping weapons
  • Gun - Has the Gun script that controls firing and reloading.

When I set the scale.x to -1 on the Player root (the body sprite) it flips every child including positions. I only worry about the rotation input for the arm when the player is flipped as it also flips how it rotates. I also have the body, head, arms, legs and feet as separate sprites.

Not sure how your player is setup, I have this kind of setup for a few different projects and have had no issues.