My Question Is How Do I Get My Player To Move In The Direction The Camera Is Facing?

I am very new to unity and everything I have done in my game so far has been done by following tutorials on YouTube but now I am at a point to where tutorials are not telling me what I need to know so I feel like it is time that I turn to the forums for answers. Anyhoo, I have this code for my third person camera and it works great but my problem is I do not know what I need to add to this code to get my player to move in the direction that the camera is facing. say for example I move my mouse to the left I want the player to rotate with the mouse and follow the camera to the left and if I move my mouse to the right I want the player to rotate with the mouse and follow the camera to the right.

This is the code I have so far.

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

public class ThirdPersonCamera : MonoBehaviour
{
public float MouseSensitivity = 10;
public Transform Target;
public float DistanceFromTarget = 2;
public Vector2 PitchMinMax = new Vector2(-40, 85);

public float RotationSmoothTime = .12f;
Vector3 RotationSmoothVelocity;
Vector3 CurrentRotation;

public Transform Pivot;

float yaw;
float pitch;

void LateUpdate()
{

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

yaw += Input.GetAxis("Mouse X") * MouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * MouseSensitivity;
pitch = Mathf.Clamp(pitch, PitchMinMax.x, PitchMinMax.y);

CurrentRotation = Vector3.SmoothDamp(CurrentRotation, new Vector3(pitch, yaw), ref RotationSmoothVelocity, RotationSmoothTime);

transform.eulerAngles = CurrentRotation;
transform.position = Target.position - (transform.forward * DistanceFromTarget);

}
}

If you can tell me what I should add to this code and how I would implement it into the code so it functions properly I would greatly appreciate it. Thanks in advance.

@HaGGGames

“I do not know what I need to add to this code to get my player to move in the direction that the camera is facing”

Hi, I didn’t check your code but you can get your camera forward direction using:
Unity - Scripting API: Transform.forward

Then you can start to move and/or turn your character’s facing towards this direction using different methods;

  • Set transform.rotation directly
  • Apply rotation values using transform.Rotate
  • Use transform.LookAt to look towards some direction

Also, you could also use (in some cases) Quaternion rotation class to get rotation that you can then use with transform.rotation and rotate:

Vector3 too has useful methods:

1 Like

Thanks for your reply I appreciate it my friend. I am going to play around with this and see if I can get it to do what I want it to do.

one more thing you can do is multiply the camera’s rotation by the input as a Vector3.

if you’re not familiar with this, yes, i said rotation(Quaternion) times direction(Vector3).

Let’s assume you have an input like so:

Vector3 input = new Vector(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));

the x axis is our side to side input the z axis is our front-back input, if we use this like this for example:

transform.position += input;

this will be relative to the global coord system, to make it relative to the local coord system you can take the object that is rotated on the Y to face the player and do

Vector3 input = Quaternion.Euler (0, player.transform.eulerAngles.y, 0) * new Vector(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));

*Quaternion must come first in multiplication with Vector3

and that makes the input axis spin to the players facing.

if the math freaks you out you could also take advantage of stuff like transform.TranformDirection to do that for you.

PS why is your code in LateUpdate rather than Update?
and what does this mean?

        transform.position = Target.position - (transform.forward * DistanceFromTarget);

also, you’re using code tags wrong, but close enough :wink:

4 Likes

@SparrowsNest

“transform.position = Target.position - (transform.forward * DistanceFromTarget);”

Didn’t check the context, but I think this in itself place object facing towards target with it’s forward axis, positioned away along it’s forward axis, by distanceFromTarget’s length, centered relative to target.position.

This can be used for pan/tilt camera setup that is targeting some object = a camera that rotates on virtual sphere around some target.

It looks to me like it’s setting the position of the transform to the position of the target with an offset of ‘DistanceFromTarget’ in what every direction .forward points to.

question is, is this the intended behavior?

@SparrowsNest

For that OP can answer, but I’m pretty sure it works like that, as I just happened to work with such rotation setup today. No idea if it’s intended behavior.

[QUOTE=

PS why is your code in LateUpdate rather than Update?
and what does this mean?

        transform.position = Target.position - (transform.forward * DistanceFromTarget);

also, you’re using code tags wrong, but close enough ;)[/QUOTE]

Reason its in late update is because the guy in the tutorial told me to do so but I put it in Update Now, and when I took that piece of code out the camera was no longer following the player so I assume that piece of the code is making the camera follow the player lol, Srry Im a noob to coding just piecing things together and trying to make it work, seeing what everything does and learning as I go but I figured out how to get the player to move with the camera I added this piece of code to my PlayerMovement Script and it did what I wanted it to do so I am happy Lol

    transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
                       Camera.main.transform.localEulerAngles.y, transform.localEulerAngles.z);
2 Likes