- Home /
How to make my player walk according to the cameras direction?
I build a third person game with an orbital camera. The player is moving on a Z/X plane. I want the player to walk forward (or any direction) according to the camerasview(Rotation) but only on the Z/X plane on which the player walks on. The Camera should work as a way to explore the environment by orbiting around the player and not moving anything by doing so. Haven't found any soulution yet that fit my game. PlayerController Class:
//Moving
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * Playerspeed, 0f, Input.GetAxis("Vertical") * Time.deltaTime * Playerspeed, Space.World);
Camera Controller Class:
{
public Transform target;
public Vector3 offset;
public float zoomSpeed = 4f;
public float minZoom = 5f;
public float maxZoom = 15f;
public float pitch = 2f;
public float yawSpeed = 100f;
private float currentZoom = 10f;
private float currentYaw = 0f;
void Update()
{
currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
currentYaw -= Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
}
void LateUpdate()
{
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
transform.RotateAround(target.position, Vector3.up, -currentYaw);
}
}
Answer by SunnyChow · Oct 31, 2018 at 04:22 AM
Vector3 forward = Camera.main.transform.forward;
forward.y = 0;
forward.Normalize();
Vector3 right= Camera.main.transform.right;
right.y = 0;
right.Normalize();
transform.position += Input.GetAxis("Horizontal") * Time.deltaTime * Playerspeed*right + Input.GetAxis("Vertical") * Time.deltaTime * Playerspeed*forward;
That works fine but now I have a problem with the Playercharacter alignment (because it still depends on the players local movement and not the cameras or the global movement ect.)
//Alignment-Rotation
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal , 0f, moveVertical);
if (moveHorizontal != 0 || moveVertical != 0) //align only when moving
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.20F); //smooths the rotation-alignment and makes the player face the...
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612181244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third Person Camera Help? 0 Answers
Movement with Camera 2 Answers
Rigidbody Moveposition not working when Camera follows object? 0 Answers
Camera Relative Movement 2 Answers
First person camera restriction. 1 Answer