Help with controlling the player characters movement (how to walk)

Hi,
I hope this is the right place to ask this. I’ve already gone through the beginners 2D kit and created my scene, placed my player character and even added background music, gravity and an idle animation.
But I can’t seem to figure out how to make my character move right and left (walking). I’ve read and tried a few scripts but since I’ve got no knowledge of C#, I have to ask this here. Is there a simple way for controlling the character?

Thank you for your help : )

Hi, the easiest way i know is this:

public float playerSpeed;     //Variable to store the player's movement speed
private Rigidbody2D playerRigidBody2D;  

void Start()
    {
        playerRigidBody2D = GetComponent<Rigidbody2D>();    //Link the rigidbody component of the player to the variable playerRigidBody2D
   
    }


void Update()
    {
        /*************************
        * Player Movement System *
        *************************/
        float moveHorizontal = Input.GetAxisRaw("Horizontal");     //Get the current horizontal input and store it in the variable moveHorizontal
        float moveVertical = Input.GetAxisRaw("Vertical");         //Get the current vertical input and store it in the variable moveVertical

        Vector2 movement = new Vector2(moveHorizontal, moveVertical);   //Create a vector2 with the 2 variables to use it in the .AddForce function

        playerRigidBody2D.AddForce(movement * playerSpeed);    //Call the AddForce function of the player RigidBody2D and use the vector2 movement to tell in which direction the force must be applied
                                                               //and the varabiable playerSpeed that rapresent how strong the force is
    }

This script moves the player thanks to the rigidbody2d component and .addforce, there are also other ways to move the character

1 Like

With all respect, Nyfirex’s solution has several problems with it and you should not use it. Most importantly:

  1. Never interact with the physics system outside of the FixedUpdate loop. In this case the Rigidbody2D is being modified in Update.
  2. It controls both vertical and horizontal movement in the way I would expect in a top-down game, but the OP seems to be making a platformer.

Here’s something similar:

using UnityEngine;

/// <summary>
/// Handles translating player input into movement.
/// </summary>
public class PlayerMovementController : MonoBehaviour
{
    /// <summary>
    /// The player's movement speed.
    /// </summary>
    public float movementSpeed;

    /// <summary>
    /// The direction of movement on the horizontal axis.
    /// </summary>
    private float horizontalInput;

    /// <summary>
    /// The rigidbody we're controlling.
    /// </summary>
    private Rigidbody2D rb;

    private void Start()
    {
        this.rb = this.GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        // We collect our horizontal input here and store it. This is another
        // general principle - you should generally only interact with the
        // Input system in Update. You could get away with doing this within
        // FixedUpdate in this case, but you might as well start using best
        // practices.
        this.horizontalInput = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        // We get our current velocity here.
        Vector2 velocity = this.rb.velocity;

        // Since we only want to modify our horizontal velocity, so we do so
        // here.
        velocity.x = this.horizontalInput * this.movementSpeed * Time.deltaTime;

        // We can now apply our velocity directly.
        this.rb.velocity = velocity;
    }
}

As an addendum: you may not want to directly modify the velocity of the rigidbody, in which case you can use AddForce. This will make it more complicated, though, because you’ll need to start considering other aspects, i.e. what should the maximum speed of the player be? If you’re applying forces, you’ll be able to accelerate indefinitely without bounding it somehow.

Edit: Fixed an oopsie in the FixedUpdate function.

1 Like