Sprite Renderer not flipping the sprite

Hi, I was trying to tidy up my code for my player movement script, and firing a bullet script into one master script. Both the ‘Player Movement’ script and the ‘Bullet Emitter’ script worked fine on their own, and all I have done is organise it all into one script named ‘Player’. I had debugged all the way through the script and they all came up in the console. Can anyone help, all I want it to do if flip the sprite when walking left, and fire right when facing right. Because at the moment, the player can walk both ways, but the sprite stays facing right, even when walking left, and the bullet only fires left, even when walking right. I am also using PS4 movement scripts.

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

public class Player : MonoBehaviour
{
[Header(“Speed Settings”)]
public float walkingSpeed;
public float runningSpeed;
public float sneakingSpeed;
public float idleSpeed;
public float jumpingSpeed;

[Header("RenderCam")]
public RenderCamera renderCam;

[Header("Firing Bullet")]
public GameObject Bullet_Emitter_Right;
public GameObject Bullet_Emitter_Left;
public GameObject Bullet;
public float Bullet_Forward_Force;

private Animator anim;
SpriteRenderer mySpriteRenderer;

private void Awake()
{
    mySpriteRenderer = GetComponent<SpriteRenderer>();

    anim = GetComponent<Animator>();
}

private void Update()
{
    //Sprite Flipping
    if(Input.GetKeyDown(KeyCode.A))
    {
        if (mySpriteRenderer != null)
        {
            mySpriteRenderer.flipX = true;
        }
    }

    if (Input.GetAxis("leftstick1horizontal") < 0)
    {
        if (mySpriteRenderer != null)
        {
            mySpriteRenderer.flipX = true;
        }
    }

    if (Input.GetKeyDown(KeyCode.D))
    {
        mySpriteRenderer.flipX = false;
    }

    if (Input.GetAxis("leftstick1horizontal") > 0)
    {
        mySpriteRenderer.flipX = false;
    }

    //Which way the sprite is facing, then firing
    if(mySpriteRenderer.flipX = true && Input.GetKeyDown(KeyCode.Mouse0))
    {
        GameObject Temporary_Bullet_Handler;
        Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter_Left.transform.position, Bullet_Emitter_Left.transform.rotation) as GameObject;

        Temporary_Bullet_Handler.transform.Rotate(Vector3.right * 90);

        Rigidbody Temporary_RigidBody;
        Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();

        Temporary_RigidBody.AddForce(-transform.right * Bullet_Forward_Force);

        Destroy(Temporary_Bullet_Handler, 0.5f);
    }

    if(mySpriteRenderer.flipX = false && Input.GetKeyDown(KeyCode.Mouse0))
    {
        GameObject Temporary_Bullet_Handler;
        Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter_Right.transform.position, Bullet_Emitter_Right.transform.rotation) as GameObject;

        Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);

        Rigidbody Temporary_RigidBody;
        Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();

        Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);

        Destroy(Temporary_Bullet_Handler, 0.5f);
    }
}

void FixedUpdate()
{
    if(idleSpeed == 0)
    {
        anim.SetFloat("Speed", idleSpeed = 0);
    }

    //PC Movement for moving right
    if(Input.GetKey(KeyCode.D))
    {
        //Sprinting
        if (Input.GetKey(KeyCode.LeftShift))
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * runningSpeed;
        }

        //Sneaking
        else if (Input.GetKey(KeyCode.LeftControl))
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * sneakingSpeed;
        }

        //Walking
        else
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * walkingSpeed;
            anim.SetFloat("Speed", walkingSpeed);
        }
    }

    //PS4 Movement for moving right
    if (Input.GetAxis("leftstick1horizontal") > 0)
    {
        if (Input.GetKey(KeyCode.Joystick1Button1))
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * sneakingSpeed;
        }

        else if (Input.GetKey(KeyCode.Joystick1Button8))
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * runningSpeed;
        }

        else
        {
            transform.position += transform.TransformDirection(Vector3.right) * Time.deltaTime * walkingSpeed;
            anim.SetFloat("Speed", walkingSpeed);
        }
    }

    //PC Movement for moving left
    if(Input.GetKey(KeyCode.A))
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * runningSpeed;
        }

        else if (Input.GetKey(KeyCode.LeftControl))
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * sneakingSpeed;
        }

        else
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * walkingSpeed;
            anim.SetFloat("Speed", walkingSpeed);
        }
    }

    //PS4 Movement for moving left
    if (Input.GetAxis("leftstick1horizontal") < 0)
    {
        if (Input.GetKey(KeyCode.Joystick1Button1))
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * sneakingSpeed;
        }

        else if (Input.GetKey(KeyCode.Joystick1Button8))
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * runningSpeed;
        }

        else
        {
            transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * walkingSpeed;
            anim.SetFloat("Speed", walkingSpeed);
        }
    }

    //PC Jumping
    if (Input.GetKey("space"))
    {
        transform.position += transform.TransformDirection(Vector3.up) * Time.deltaTime * jumpingSpeed;
    }

    //PS4 Jumping
    if (Input.GetKey(KeyCode.Joystick1Button0))
    {
        transform.position += transform.TransformDirection(Vector3.up) * Time.deltaTime * jumpingSpeed;
    }

    //RenderCam Snapshot
    if (Input.GetKeyDown(KeyCode.C))
    {
        renderCam.CallTakeSnapshot();
    }
}

}

Hi @ollyrowlands1

When you’re trying to check ‘which way the sprite is facing’, you’re assigning value to mySpriteRenderer.flipX:

//first part
if (mySpriteRenderer.flipX = true && Input.GetKeyDown(KeyCode.Mouse0))
{
    //code
}
//second part
if (mySpriteRenderer.flipX = false && Input.GetKeyDown(KeyCode.Mouse0))
{
    //code
}

So instead of checking that value with equal operator, you’re overriding previous operations (where you took Input to set proper value of flipX) and then taking that overrided value for IF statement.
That means flipX variable always ends with a false value (second part) - that’s why you always face right.
That also means that only the first part of code executes its IF block - that’s why you always fire left.

Just change = to == in both IF statements.