Help My movement is broken!!

I’m making a FPS and i need help with the movement i already have a script for locking my mouse and making my player follow it but the movement it broken if i look one way it doesn’t work like it looks that way but it doesn’t move that way when I press W Plz help!!

Sure thing here you go using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Movement : MonoBehaviour
{
CharacterController characterController;

public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;

private Vector3 moveDirection = Vector3.zero;

void Start()
{
    characterController = GetComponent<CharacterController>();
}

void Update()
{
    if (characterController.isGrounded)
    {
       

        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
        moveDirection *= speed;

        if (Input.GetButton("Jump"))
        {
            moveDirection.y = jumpSpeed;
        }
    }

    
    moveDirection.y -= gravity * Time.deltaTime;

   
    characterController.Move(moveDirection * Time.deltaTime);
}

}

Thank you for showing your code.

Take a look at this one:

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

public class Movement : MonoBehaviour
{
    CharacterController characterController;

    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    private Vector3 moveDirection = Vector3.zero;
    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }
    void Update()
    {
        if (characterController.isGrounded)
        {
            float xDir = Input.GetAxis("Horizontal");
            float zDir = Input.GetAxis("Vertical");
            moveDirection = transform.right * xDir + transform.forward * zDir;
            moveDirection.y = 0;        // Otherwise we will fly towards camera direction if it points upwards

            moveDirection *= speed;
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        moveDirection.y -= gravity * Time.deltaTime;

        characterController.Move(moveDirection * Time.deltaTime);
    }
}

Why in your code you didn’t move towards forward direction of your view is because you never used it in code.

In the code abow we calculate Horizontal and Vertical input separately and then calculate overall moveDirection by multiplying input values to our right and forward direction of transform what makes it take into account our transform orientation. However, we shouldn’t forget to reset moveDirection.y to 0, otherwise if we look slightly upwards or downwards it will fly into that direction.


Hope that helps.