i need help with making the player move in the direction he looks

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

public class NewBehaviourScript : MonoBehaviour
{
    public Transform groundCheckTransform;
    private bool jumpKeyWasPressed;
    private float horizontalInput;
    private float verticalInput;
    private Rigidbody rigitbodyComponent;
    public LayerMask playerMask;

    // Start is called before the first frame update
    void Start()
    {
        rigitbodyComponent = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpKeyWasPressed = true;
        }

        horizontalInput = Input.GetAxis("Horizontal");

        verticalInput = Input.GetAxis("Vertical");
    }

    private void FixedUpdate()
    {
        rigitbodyComponent.velocity = new Vector3(horizontalInput * 10, rigitbodyComponent.velocity.y, verticalInput * 10);

        if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0)
        {
            return;
        }

        if (jumpKeyWasPressed)
        {
            rigitbodyComponent.AddForce(Vector3.up * 8, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
        }

        float moveHorizontal = Input.GetAxisRaw("Horizontal");
        float moveVertical = Input.GetAxisRaw("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        transform.rotation = Quaternion.LookRotation(movement);


        transform.Translate(movement * 5 * Time.deltaTime, Space.World);

    }

}

internal class SerializedFieldAttribute : Attribute
{
}

this is the first code i’ve ever written so how do i fix this

I assume you watched a tutorial.

Well, few things to consider:

  • Do you have any errors?
  • Have you tried to work it out
  • Does the code even work, if no, then print debug.log statements around your code to see what is working and what Isn’t

the code works the problem is when i press “W” i want him to walk the direktion the camera is pointet.
currently he just walks north

Maybe thats happens because the camera is facing north???

Basically you need to use the heading of the camera to rotate the inputs.

This is a script out of a fully-functioning project you can download at this location:

Notable lines above:

Line 124 - get the camera heading

Line 126 - construct a rotation

Line 128 - produce the rotated controls from the unrotated controls

Line 130 - use them