My first person movement controller will not move sideways

I started unity today. I wanted to make a first person controller, so I went into the learn area to see if I could find anything. I found some movement code, so I copied it into a script and made some adjustments. I then, when testing, realized that I couldn’t move side to side. Can someone help?

Here’s the code:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float jumpSpeed = 30.0f;


    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        float moveVertical = Input.GetAxis("Vertical");
        Vector3 movement = transform.forward * moveVertical * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);

        bool moveHorizontalOne = Input.GetKey("d");
        Vector3 moveRight = transform.right * moveHorizontalOne * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);

	bool moveHorizontalTwo = Input.GetKey("a");
        Vector3 moveLeft = -transform.right * moveHorizontalTwo * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);
    }
}

It might be a bit of a large first step if you just started today but… try commenting out all the code in FixedUpdate(). Clearly nothing happens as one would expect. Then uncomment the moveVertical stuff. You should notice you named the Vector3 “movement” not differentiating it. No problem yet but it will turn into one.

Now comment that again and uncomment the moveHorizontalOne section. Hey what happened to the movement Vector3 and why is nothing using moveRight?

And don’t name things moveHorizontalOne and moveHorizontalTwo if they have something to do with left and right.

Making a CC is kind of a big tangly step.

But if you wanna debug what you have, see below for debugging.

If you just want a functioning character controller, here’s a great starter:

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!

If you want to stick with what you have it just sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

Check input in Update(), not FixedUpdate(). Though the code may have other issues too. I don’t see the need for the bools, just do
if (Input.GetKey) { }
Use ChatGPT to help you. It’s invaluable for stuff like this. Give it a prompt like

“create a unity script for basic player/rigidbody control using standard keyboard input, with speed and jumpSpeed parameters”

I can 99% guarantee the first thing it produces will work. Then read through it and ask questions about anything you don’t understand. You can also paste your code and ask it to tweak/correct it, which is another good approach.

Here’s what it gave back to me when I put in your code and asked it to fix any issues. I haven’t tested it.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float jumpSpeed = 30.0f;

    private Rigidbody rb;
    private float moveVertical;
    private float moveHorizontal;
    private bool jump;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        // Get input in Update
        moveVertical = Input.GetAxis("Vertical");
        moveHorizontal = Input.GetAxis("Horizontal");
        jump = Input.GetKeyDown(KeyCode.Space);
    }

    private void FixedUpdate()
    {
        // Apply movement
        Vector3 movement = (transform.forward * moveVertical + transform.right * moveHorizontal) * speed * Time.fixedDeltaTime;
        rb.MovePosition(rb.position + movement);

        // Handle jumping
        if (jump && Mathf.Abs(rb.velocity.y) < 0.01f) // Simple grounded check
        {
            rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
        }
    }
}