Flipping/Rotation Issue

Hi, I am trying to make a game that will involve a shooting mechanic. I already have a solid player movement, animation and enemies. However, I am encountering a problem with the shooting. The mechanic works and I can spawn the bullets, but I cannot figure out how to flip the shooting point, video is here: https://drive.google.com/file/d/1xEoUl_IXf8e4E6nZwgyiugaM-YyaDSKZ/view?usp=sharing

This video shows the script I’ve used, any help as to why this is happening and how to fix it would be appreciated. 2023-07-12 14-24-46.mkv - Google Drive

Side note: I have tried replacing the flip.x with transform.Rotate(0, 180, 0) and while that works somewhat, it causes the character to glitch and the shooting point does not always change direction with the player.

What do you mean flip the shooting point? If the shooting point is an empty gameobject as a child of the player/weapon, then it should flip with the player flipping. Do you mean the direction of the projectiles arent changing or something else?

Yes, the projectile direction isn’t changing

Don’t show your script in a video, just post it here (and please do so properly !).

But also I reaffirm what I said before. Yes the shooting point object is a child of the player, and yet when I play the game and switch to the scene view, the point does not flip
9144661--1270735--upload_2023-7-13_17-11-58.png
9144661--1270738--upload_2023-7-13_17-12-14.png
See?

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

public class PlayerMovement : MonoBehaviour
{
[SerializeField]
public float moveForce = 10f;

[SerializeField]
public float jumpForce = 11f;

private float movementX;

private Rigidbody2D rb;
private Animator anim;
private SpriteRenderer sr;
private string WALK_ANIMATION = “Walk”;
private bool grounded;

public Transform shootPoint;

Object bulletPrefab;

public AudioSource gunfire;

private void Awake()
{
rb = GetComponent();
anim = GetComponent();
sr = GetComponent();
}

void Start()
{
bulletPrefab = Resources.Load(“Laser”);
}

private void Update()
{
PlayerMoving();
AnimatePlayer();
Shoot();
}

void PlayerMoving()
{
movementX = Input.GetAxisRaw(“Horizontal”);

transform.position += new Vector3(movementX, 0f, 0f) * Time.deltaTime * moveForce;

if (Input.GetKey(KeyCode.W) && grounded)
{
Jump();
}
}

void AnimatePlayer()
{
if(movementX > 0)
{
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = false;

}

else if (movementX < 0)
{
anim.SetBool(WALK_ANIMATION, true);
sr.flipX = true;

}

else
{
anim.SetBool(WALK_ANIMATION, false);

}

anim.SetBool(“Grounded”, grounded);
}

private void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
grounded = false;
}

private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == “Ground”)
{
grounded = true;
}
}

void Shoot()
{
if (Input.GetKey(KeyCode.Space))
{
gunfire.Play();
anim.Play(“Shoot”);
}
}
}

The script for how the player moves

I tried… :frowning:

I posted the code, what’s the problem!?

/CODE]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField]
    public float moveForce = 10f;

    [SerializeField]
    public float jumpForce = 11f;

    private float movementX;

    private Rigidbody2D rb;
    private Animator anim;
    private SpriteRenderer sr;
    private string WALK_ANIMATION = "Walk";
    private bool grounded;

    public Transform shootPoint;

    Object bulletPrefab;

    public AudioSource gunfire;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        sr = GetComponent<SpriteRenderer>();
    }

    void Start()
    {
        bulletPrefab = Resources.Load("Laser");
    }

    private void Update()
    {
        PlayerMoving();
        AnimatePlayer();
        Shoot();
    }

    void PlayerMoving()
    {
        movementX = Input.GetAxisRaw("Horizontal");

        transform.position += new Vector3(movementX, 0f, 0f) * Time.deltaTime * moveForce;

        if (Input.GetKey(KeyCode.W) && grounded)
        {
            Jump();
        }
    }

    void AnimatePlayer()
    {
        if(movementX > 0)
        {
            anim.SetBool(WALK_ANIMATION, true);
            sr.flipX = false;
           
        }

        else if (movementX < 0)
        {
            anim.SetBool(WALK_ANIMATION, true);
            sr.flipX = true;
           
        }

        else
        {
            anim.SetBool(WALK_ANIMATION, false);
           
        }

        anim.SetBool("Grounded", grounded);
    }

    private void Jump()
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        grounded = false;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Ground")
        {
            grounded = true;
        }
    }

    void Shoot()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            gunfire.Play();
            anim.Play("Shoot");
        }
    }
}

You don’t modify the Transform if you’re using 2D physics, it’s completely bypassing what the Rigidbody2D is trying to do.

You are flipping a SpriteRenderer. Why would you expect that to modify the Transform hierarchy? It won’t. It’ll flip the SpriteRenderer.

Sorry, I was being a bit of a jerk there. I see you’ve got it straightened out now though! If the code isn’t inside code blocks it’s just a pain to read since everything is left-aligned.

So, there isn’t a way to fix the issue with my current code?

Fair enough, sorry about my hasty posting. I’m still relatively new to Unity and there’s a lot I don’t understand, and I have a habit of getting agitated when the stuff doesn’t work

Rotate the transform hierarchy 180-deg or flip the local scale. Just don’t do that continuously because you don’t want it to constantly flip when you’re moving in a direction. You only want to do that when you change direction.

You’re doing this:

rb.velocity = new Vector2(rb.velocity.x, jumpForce);

The Rigidbody2D won’t have any velocity X, you’re just stomping over the Transform teleporting it from position to position, it isn’t physically moving.

There’s lots of tutorials showing basic physics movement/jumping though so I would highly recommend going through some of those.

I have tried using transform.Rotate(0, 180,0) and it works somewhat, but it does result in my character constantly flipping when the key is pressed, it does flip the point though

So how would I rotate the hierarchy NOT continuously?

Here’s one technique you could try:

        if (movementX < 0)
        {
            // Make X scale negative
            Vector2 negativeXScale = transform.localScale;
            negativeXScale.x = -Mathf.Abs(negativeXScale.x);
            transform.localScale = negativeXScale;
        }
 
        else if (movementX > 0)
        {
            // Make X scale positive
            Vector2 positiveXScale = transform.localScale;
            positiveXScale.x = Mathf.Abs(positiveXScale.x);
            transform.localScale = positiveXScale;
        }

Thanks, gonna take a break for the rest of today but will attempt tomorrow

Well you must be using different code than you’ve posted then which isn’t helping. If you flip the Transform in the “AnimatePlayer” as you’ve shown (right now you’re setting the SpriteRenderer flip) then it will absolutely flip constantly as that runs all the time you’re moving left or right as the only condition to run it is if movement is >0 or <0. You don’t show you setting that in the code above at all so some other code you’ve not posted I can presume.

You said it wasn’t doing that and didn’t do that.

I’ll leave you to figure out the suggested code above although I would highly recommend following some basic 2D physics movement tutorials online as it’ll help a lot.