Help make player movement relative to camera facing direction. (Beginner)

Hello, I have only just started my unity coding experience and have little to no idea of how most things work yet, I have started off with pretty basic movement tutorials to get where I am at in one of my movement scripts but I am struggling with making the players facing direction relative to where the camera is facing. This is my entire movement script so far, any feedback would help or even any moderations to improve the efficiency of the code would greatly help me out. Again, I have no idea what I am doing so sorry if this code hurts to see in the sloppiness sense.

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float Speed = 1.5f;
    [SerializeField] private float sprintSpeed = 2f;
    [SerializeField] private float JumpHeight = 7f;
    [SerializeField] private float groundcheckdistance;
    [SerializeField] private float BufferCheckDistance = 0.1f;
    [SerializeField] private bool PlayerGrounded = true;
    [SerializeField]private float PlayerSensitivity = 100f;
    [SerializeField] private float sensMultiplier = 1f;
    private float desiredX;
    private float xRotation;

    public Transform playerCam;
    public Transform orientation;
    private Rigidbody rb;

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

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    private void Update()
    {
        PlayerMovements();
        sprinting();
        Jumping();
        GroundedCheck();
        LookRotations();
    }

    private void PlayerMovements()
    {
        float HorizontalInput = Input.GetAxis("Horizontal");
        float VerticalInput = Input.GetAxis("Vertical");
        Vector3 Movement = new Vector3(HorizontalInput, 0f, VerticalInput) * Speed;
        rb.MovePosition(rb.position + Movement * Time.fixedDeltaTime);
    }

    private void sprinting()
    {
        if(Input.GetKey(KeyCode.LeftShift) == true && PlayerGrounded)
        {
            Speed = sprintSpeed;
        }
        else
        {
            Speed = 1.5f;
        }
    }

    private void Jumping()
    {   
        if(Input.GetButtonDown("Jump") && PlayerGrounded)
        {
            rb.AddForce(new Vector3(0f, JumpHeight, 0f), ForceMode.Impulse);
            PlayerGrounded = false;
        }
    }

    private void GroundedCheck()
    {
        groundcheckdistance = (GetComponent<CapsuleCollider>().height / 2) + BufferCheckDistance;
        RaycastHit GroundHit;
        if (Physics.Raycast(transform.position, -transform.up, out GroundHit, groundcheckdistance))
        {
            PlayerGrounded = true;
        }
        else
        {
            PlayerGrounded = false;
        } 
    }

    private void LookRotations()
    {
        float mouseX = Input.GetAxis("Mouse X") * PlayerSensitivity * Time.fixedDeltaTime * sensMultiplier;
        float mouseY = Input.GetAxis("Mouse Y") * PlayerSensitivity * Time.fixedDeltaTime * sensMultiplier;

        Vector3 rot = playerCam.transform.localRotation.eulerAngles;
        desiredX = rot.y + mouseX;
        
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
        orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
    }
}

Is this a 3rd person game? Like do you want the player to face where the mouse is pointing on the screen?

Or is this a first person shooter and you want the player to look to the left as the mouse moves to the left?

I THINK it sounds like you want the player to move relative to the camera, so I would suggest you go about this in a totally different manner.

Break this into 2 problems.

  1. You need to get the ground reference point of where the player wants to look based on the mouse position.
  2. You want the player to be able to look at a fixed point around them.

In order to get the mouse position you could do a raycast and create an invisble plane on a unique layer that’s always at the players feet so only the plane intersects with the settings of the raycast. That will give you the location of where you want the player to look.

You could put a sphere in the scene and move it in realtime and see if you can get the player to look at that. And once you have that working you could start with the other problem and spawn spheres based on the raycast hitting a location until you’re able to see it working and use this reference point to see if you can get the player rotating to face that point.

You could use the angle difference to see the y orientation you want to get the player looking towards that angle. You might have to mess around between transform.forward, or transform.right, or maybe something different to see which one works for your setups.

Replace line 40 with:

rb.AddRelativeForce(movement);
1 Like

Yes this is a first person 3d shooter style game, i just want the player to spin in the direction the camera is facing and also make it so when the player looks around the movement of the player is still normal because currently if i turn around all the controls get inverted if that makes any sense at all

Ah ok.

Well have you tried greatly reducing the sensitivity? If a sensitivity is too high it can wrap all the way around and you’ll end up with a revered rotation. And if that doesn’t solve it, maybe just subtract the xrotation rather than adding it?

Any chance you could upload a small video on YouTube showing the issue? It helps to visualize what the problem is.