Need help with a Horizontal movement issue

I got the code half right i guess. i only have one script in my project. character moves back and forth and jumps around fine. Only issue is he doesnt stay forward in each direction he turns to. yes i followed a video tutorial and i looked over the code 6 times and nothing seems different from what the guy did. But anyway heres the entire “Playermovement” code. I think its the transform local scale that could be altered better. My character is a 3 frame 8 bit style character. just think of it as like a Megaman or Castlevania NES character. Thats the movement and look im trying to get right

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

public class PlayerMovement : MonoBehaviour{

    public AudioClip jumpAudio;
    public AudioClip respawnAudio;
    public AudioClip ouchAudio;

    public float movementSpeed;
    public Rigidbody2D rb;
    public Animator anim;

    public float jumpForce = 20f;
    public Transform feet;
    public LayerMask groundLayers;

    float mx;
  

    private void Update() {
        mx = Input.GetAxisRaw("MoveHorizontal");

        if (Input.GetButtonDown("Jump") && IsGrounded()) {
            Jump();
        }

        if (Mathf.Abs(mx) > 0.05f){
            anim.SetBool("isRunning", true);
        } else {
            anim.SetBool("isRunning", false);
        }

        if (mx > 0f) {
           transform.localScale = new Vector3(1f, 1f, 1f);
        } else if (mx < 0f) {
           transform.localScale = new Vector3(-1f, 1f, 1f);
        }

        anim.SetBool("isGrounded", IsGrounded());
    }

    private void FixedUpdate() {
        Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);

        rb.velocity = movement;
    }

    void Jump() {
        Vector2 movement = new Vector2(rb.velocity.x, jumpForce);

        rb.velocity = movement;
    }

    public bool IsGrounded() {
        Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);

        if (groundCheck != null){
            return true;
        }

        return false;
    }
}

Are you using an analog input like a joystick? If so, then GetAxisRaw will pretty much never return 0, it’ll always be either > 0f or < 0f. You should use GetAxis, which includes a dead zone, or else program your own dead zone (perhaps by adding mx = 0; between lines 32 and 33).

yes im using the Xbox one analog to move the character but also the issue happens with the keyboard too. i tried those changes you mentioned but oddly enough it still happens. Basically when i try to make my character walk left it auto flips animation to the right.

Your game looks decent anyway lad :slight_smile:

Is it your first attempt?

Be sure you have actually unplugged the Xbox controller for this test. Otherwise, it’s still there sending axis values that are almost-but-not-exactly zero even when you’re not using it.

Next, check your assumptions by littering your code with Debug.Log statements. :slight_smile: In particular, put one next to the line that sets localScale to (1,1,1). Then study the console output and see if it really is that line that’s doing it. If so, you can log the variables (such as mx) that must have different values than what you assume. If not, then you know there’s actually some other code (perhaps in another script) that is resetting the scale.

Thanks. Yeah this is my very first struggle attempt of the year. 3rd month in so far. Figured to start off with sprites to edit and play around with for the prototype. the project will evolve the more i figure out unity scripts better

Ill try to look into what you mentioned here. ill disconnect the xbox one controller for the next test and see what happens with just the keyboard. i only have two scripts so far. one for the Camera follow which is fine and the one there for Player movement thats giving me some issues for now

Also at the time this is what happens too when he stands idle when i stop moving forward with the controls. its an on and off weird issue. i was looking at ground check and thats all on from what i saw and connected
https://www.youtube.com/watch?v=WHi4wVXPmXA

@JoeStrout you were right. it was the Xbox one controller screwing things up. when i disconnected it and just used the Keyboard everything was normal in movement. Now im wondering how do i fix that issue clean smh I know of the New UI input system but for some reason i can never get it right in the script (for now) so that the game uses it properly.

anyway thanks again for the tip. ill be tinkering more with the character movements and then go deal with Xbox one controller issue with the new input system thing later… this how it looks now moving fine without the controller

EDIT:

Weird. I just remembered i had a new 8bitDo Gamepad that has analogs. im using that now instead and theres entirely no issues moving my character. i guess its an xbox one controller problem. i assume entirely now

Yes. An analog stick is never at exactly zero; it’s only close to zero when you’re not touching it.

But I already suggested ways to fix it:

I know you did but nothing fixed it really when i did that for whatever reason. so i chucked it up as the controller is a bit out of wack now so i went to a retro analog controller i just got for retro games. the 8bitDo? it works perfect with its analog. odd but it worked perfectly like the PC keyboard. ill revisit that xbox one controller issue another time. eventually i want to test my project on the xbox series S dev kit i have. Thanks though. it got me to remove the xbox controller and look deeper anyway with something else