Trying to move the player object in the direction of the camera

I’m trying to work out a way to do third person movement where the player always moves relative to the direction of the camera. Forward is always the direction the camera is facing, etc. The camera is always centered on the player, which I implemented by having the camera parented to a rigidbody object that is always in the same position as the player; the mouse movement rotates the rigidbody and thus the camera. This part of the code works completely fine.

Camera code

using UnityEngine;
using System.Collections;

public class CameraStuff : MonoBehaviour {

    float MouseMovementX;
    float MouseMovementY;
    float MouseMovementWheel;
    public float CameraPanSpeed = 10.0f;
    public float CameraScollSpeed = 10.0f;

    Vector3 CameraPan;
    Vector3 CameraScroll;

    Rigidbody CameraFocusPoint;
    public Rigidbody Player;
    public GameObject PlayerCamera;

    Quaternion NewCameraRotation;

    void Awake () {
        CameraFocusPoint = GetComponent<Rigidbody>();
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
   
    void Update () {

        //Get inputs
        MouseMovementX = Input.GetAxis("Mouse X");
        MouseMovementY = Input.GetAxis("Mouse Y");
        MouseMovementWheel = Input.GetAxis("Mouse ScrollWheel");

        //Multiply inputs by values set in the inspector and assign them to the desired axes of the object to be controlled
        CameraPan = new Vector3(0, MouseMovementX, MouseMovementY) * CameraPanSpeed;
        CameraScroll = new Vector3(MouseMovementWheel, 0, 0) * CameraScollSpeed;

        //Create a new rotation equal to the current rotation plus the mouse input
        NewCameraRotation = Quaternion.Euler(CameraFocusPoint.rotation.eulerAngles + CameraPan);
        //Apply the new rotation
        CameraFocusPoint.rotation = NewCameraRotation;

        //Move the camera forward and backward relative to the focus point
        PlayerCamera.transform.localPosition = PlayerCamera.transform.localPosition + CameraScroll;

        //Set focus point to be in the same position as the player
        CameraFocusPoint.position = Player.position;

    }

}

My trouble is getting the player object to move relative to the camera. Every last thing I’ve tried has resulted in the player moving along the global X and Z axes. I’ve tried several ways of setting the player’s rotation to be the same as the camera focus point. The rotation works fine, but every way I’ve tried to apply movement has just moved the player along the global axes anyway. I’ve tried transform.Translate; Rigidbody.movePosition; AddForce (which did nothing, even though the player has a non-kinematic rigidbody); and manually setting the transform.position or the Rigidbody.position. I can’t figure out why nothing I’ve tried applies the movement to the object’s local axes.

This is the movement code as it currently stands.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    float MoveX;
    float MoveY;
    public float PlayerMovementSpeed = 6.0f;

    Rigidbody Player;
    public Rigidbody CameraFocus;

    float PlayerMotionX;
    float PlayerMotionY;

    Vector3 AmountToMove;

    void Awake() {
        Player = GetComponent<Rigidbody>();
    }

    void Update() {

        MoveX = Input.GetAxis("Horizontal");
        MoveY = Input.GetAxis("Vertical");

        PlayerMotionX = MoveX * 0.5f * PlayerMovementSpeed * Time.deltaTime;
        PlayerMotionY = MoveY * PlayerMovementSpeed * Time.deltaTime;
               
        AmountToMove = new Vector3(PlayerMotionX, 0, PlayerMotionY);

        Player.position = (Player.position + AmountToMove);


    }
}

I have no idea what I’m doing wrong, if anything, or if my approach here is just flawed. It’s not been very good for my sanity. I’d really appreciate any help fixing it or ideas about doing it differently.

You probably want to do something like this

Vector3 forward = Vector3.ProjectOnPlane(Camera.main.transform.forward, Vector3.up);
Player.forward = forward;
Player.Translate(AmountToMove);

That works! Though it’s jittery, and I’m not sure how to fix that because the interpolation settings on the rigidbody aren’t doing anything, but it works. Thank you so much.

If you’re using a rigidbody, you probably shouldn’t be using Translate, but rather AddRelativeForce.

Unity have some rigidbody 3rd person controller example in the asset store I think. It have an answer to your problem and some others cool features that you should check out

For no reason I can see, adding forces to the rigidbody does nothing to make it move, even though when I was trying that it was non-kinematic. For now since I’m using Translate I have the player rigidbody set to Kinematic like I believe the documentation says to. That shouldn’t cause any problems, right?

Thank you, I’ll look at that and try see how it works.

Yeah with kinematic it should be alright.