Issues with player movement

Hey all,

I’ve looked at a lot of solutions, I’ve tried out a bunch, and I’m just not finding an answer, and I’ve tried re-writing my code a few times and it just hasn’t worked out. What I’m currently working with is below, and I’ve been able to make my character shoot a projectile, I’ve been able to make them jump pretty well, but movement is being a pain.

What I’m attempting to do is make a 2D sprite move left and right with keyboard input (A and D respectively) and when it moves left and right, I want it to rotate 180 on the y so it faces the direction that it’s trying to move. Right now when I hit “D” it turns the sprite to the right, and starts moving left. When I push “A” it moves to the left and doesn’t rotate the sprite.

I know this is probably a really simple fix, but I’m very new to Unity, and I can’t seem to figure this out. I did have a basic movement script before using transform.translate and that allowed me to move left and right but it seemed like the scripting I have now is more realistic?

Also if there’s any way I can clean this up and make it better, I’d love to hear. Thank you all in advance!

using UnityEngine;

public class PlayerControl : MonoBehaviour
{
private Rigidbody2D playerRb;

public GameObject shotPrefab;
public Vector2 speed = new Vector2(10, 50);
public Vector3 turnRotation = new Vector3(0, 180, 0);

public float horizontalInput;
public float verticalInput;
public float jumpForce;
public float gravityModifier;

public bool isOnGround;

// Start is called before the first frame update
void Start()
{
    playerRb.GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update()
{
    horizontalInput = Input.GetAxis("Horizontal");

    // Move the player using interface

    Vector3 movement = new Vector3(speed.x * horizontalInput, 0, 0);

    movement *= Time.deltaTime;

    transform.Translate(movement);

    if (horizontalInput > 0) // Rotates character 90 degrees based on input
    {
        transform.localEulerAngles = turnRotation;
    }
    if (horizontalInput < 0)
    {
        transform.localEulerAngles = -turnRotation;
    }

    // Allows the player to jump
    if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
    {
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
        isOnGround = false;
    }

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        //Launch a projectile from the player
        Instantiate(shotPrefab, transform.position, shotPrefab.transform.rotation);

    }

}
public void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Ground")             // Verifies player is on ground

    {
        isOnGround = true;
    }
}

}

Hi! I think the best way to achieve something like this would be to have basic movement on an invisible gameobject- no rotation or flipping of any kind, just moving left, right and jumping - and giving this gameobject a child which will actually show any player sprites and their orientation.

This separation of concerns is great because the player movement scripts will never have to worry about what orientation the player is in, instead you can have a separate script on the art, that just checks which key is being pressed and flips or mirrors accordingly.

Similarly you can have a script that shoots projectiles, where all it does it remember direction faced and waits for a button press.

If you are up for the challenge, there are actually many ways of making your player feel even more fun to control. Check out this talk if you’re interested: Math for Game Programmers: Building a Better Jump - YouTube