Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

2D Character only move on Y axis

So i'm making a Platformer but my player can only jump and crouch. Everytime I want to walk, my player just stay here and don't move. Everything is okay in the input manager and i'm using Brackey's Character Controller 2D. For the movement, it's the same code that he use in his 2D Player Movement video. I'm currently using Unity 2019.3.14f1.

Best Answer

  • KrakenKraken Member
    Accepted Answer

    Yeah gonna try this cause at first, I asked the question on stackoverflow and a guy told me that there were some issues on 3.0

Answers

  • RailgunRailgun Member

    Since you didn't post a script I can only make some guesses:


    -Make sure the rigidbody doesn't have any constraints on the movement

    -Make sure you included the ground layer in the layermask on the script. If you haven't done this, it might think you are constantly airborne and I think air movement is disabled by default.

    -Make sure your input value is being received. Try hooking up your input into a Debug.Log line to see if it's registering.

  • KrakenKraken Member
    edited May 2020

    Okay, my input value is not being received. But, in the input manager, everything is fine... I don't understand...


  • RailgunRailgun Member

    Can you confirm that your controller or whatever input device you are using works? Again, try the debug.log trick

  • KrakenKraken Member

    I can confirm that it works. I tried the debug.log on my jump input and it was working...

    https://drive.google.com/file/d/1QKZ0i6Sw37Ea5B9Su5RVIT25tQoaIC3Y/view?usp=sharing

  • Yea you need to put the input.getaxisraw in the update and then in fixedUpdate do rigidbody.MovePosition(horizontalmove * speed * Time.deltaTime

  • Also for the animation I would just make it book called isRunning

  • KrakenKraken Member

    input.getaxisraw is already in update and in fixedUpdate I already have this.

    Is it ok?

    Here's the code:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;


    public class PlayerMovement : MonoBehaviour

    {


      public CharacterController2D controller;

      public Animator animator;


      public float runSpeed = 40f;


      float horizontalMove = 0f;

      bool jump = false;

      bool crouch = false;


      // Update is called once per frame

      void Update()

      {


        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;


        animator.SetFloat("Speed", Mathf.Abs(horizontalMove))

    ;

        if (Input.GetButtonDown("Jump"))

        {

          jump = true;

          animator.SetBool("IsJumping", true);

        }


        if (Input.GetButtonDown("Crouch"))

        {

          crouch = true;

        }

        else if (Input.GetButtonUp("Crouch"))

        {

          crouch = false;

        }

        if (Input.GetButtonDown("Jump"))

        {

          Debug.Log("True");

        }

       


      }


      public void OnLanding()

      {

        animator.SetBool("IsJumping", false);

      }


      public void OnCrouching(bool isCrouching)

      {

        animator.SetBool("IsCrouching", isCrouching);

      }


      void FixedUpdate()

      {

        // Move our character

        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);

        jump = false;

      }

    }

  • RailgunRailgun Member

    Hmm this code looks fine. Just to be sure, try using the debug trick with the horizontal movement specifically since that's what the issue is currently.

    Put this in your FixedUpdate method right before controller.Move is called: Debug.Log(horizontalMove);

    Make sure the console window in the editor does NOT have collapse enabled, sometimes that causes weird issues for me. You should be able to see the most recent console entries showing up at the bottom of your editor while in play mode. You want to see the number be greater than 0 while you are moving horizontally. Try increasing runSpeed as well if you need to. If it's constantly outputting 0 then you've found where your issue resides.

  • KrakenKraken Member
    edited May 2020

    It's constantly outputting zeros.

  • Then the issue lies there. Either your runSpeed is set to 0 or for some other reason is getting set to 0, or something's wrong with your input. If you're still stuck try replacing Debug.Log(horizontalMove); with Debug.Log(Input.GetAxisRaw"Horizontal"); just to test input itself. You can also try that with your runSpeed variable. Maybe also check the input manager to make sure you didn't accidentally rename it or something

  • KrakenKraken Member

    I can't put Debug.Log(Input.GetAxisRaw"Horizontal"); because i'm getting a compiling error. The input manager is ok and the runSpeed is not a problem because the problem here is that my input value is not being received.

  • Sorry, I meant Debug.Log(Input.GetAxisRaw("Horizontal"));

    How do you know runSpeed isn't the issue? Might as well give it a try if you see numbers showing up from the above code

  • Are you in the brackeys discord server by any chance? might be worth joining a voice channel there so you can show your screen to me if you are interested in that. I'm Accelerator on the server with the same profile picture

  • KrakenKraken Member

    Sorry but I don't really speak english and i'm getting the same console output by putting Debug.Log(Input.GetAxisRaw("Horizontal"));

  • What's the output, 0?

  • KrakenKraken Member

    And for the runSpeed, the value is 40 and it doesn't change when playing the game...

  • KrakenKraken Member

    Yes, 0.

  • If that's the case then I'm now wondering if your controller is messed up. Can you confirm that the joystick on your controller works in other videogames?

  • KrakenKraken Member

    Like I said, I think the controller is ok because I use the same scripts in another videogame that I made on Unity 2018.

  • If you're absolutely sure that the controller works and you use the same script in an older project and it works, then I would direct my attention towards the input manager. Any chance you can take a screenshot of Horizontal input setup? Something like this https://i.imgur.com/O2AWVQt.png

    Also, don't worry about trying to copy yours to look like mine, this one is customized a bit and might not be good for your project, just using it as an example

  • Looks good. This is strange, I'm gonna look into it for a bit

  • KrakenKraken Member

    Ok, thank you!

  • hey can you try replacing the debug message with this one to see if it works? Debug.Log(Input.GetAxisRaw("Vertical"));

    Curious about if it's just the horizontal input or all input axis

  • KrakenKraken Member

    It's working, like I said, jump and crouch are working.

  • weird. I can't find any info on this. Try making a new project without messing with the input settings or anything else then make a script and paste that Debug.Log line in the Update method and see if it works in the new project.

Sign In or Register to comment.