How to walk in direction of the main camera?
Hello, I am creating a 3D third person platformer and I have a third person camera that rotates around the player with the mouse.
I have been trying for days to try and get this to work properly but I have not came anywhere close.
What I am trying to achive is for the player to walk in the direction that the camera is looking at. I am out of options and I am resorting to asking here.
Thank you in advance for any help, it is much appreciated.
public class MoveTest : MonoBehaviour {
public float walkSpeed = 10.0f;
private CharacterController controller;
private float verticalVelcoity;
private float gravity = 30.0f;
private float jumpForce = 20.0f;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
//Jumping
if (controller.isGrounded)
{
verticalVelcoity = -gravity * Time.deltaTime;
if (Input.GetButton("Jump"))
{
verticalVelcoity = jumpForce;
}
}
else
{
verticalVelcoity -= gravity * Time.deltaTime;
}
//Movement
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis("Horizontal") * walkSpeed;
moveVector.y = verticalVelcoity;
moveVector.z = Input.GetAxis("Vertical") * walkSpeed;
controller.Move(moveVector * Time.deltaTime);
}
}
have you looked at the 3rd person User controller in the Standard Assets Example Project? It moves the character relative to the camera. It would be a good place to start if you don't want to use it directly.
Answer by PixelSpice · Jul 09, 2017 at 12:50 AM
Our project is actually a third person project, and getting relative camera/player information can be a pain sometimes. In our case, we have a camera that can orbit freely around the player, and all the input is relative to that camera.
In other words, if you push forward you go forward relative to the camera. If you push left the character walks to the left (but is still walking straight ahead.)
I'm not sure exactly what you're looking for, but it sounds like you are doing more or less the same thing. this is how we are doing it:
The way we do it is convert our input to an angle (we use an input value of x and y from a gamepad):
float angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg;
We then calculate the difference between the angle of the player and the angle of the camera (CameraRigTarget is a game object and used as a desired location for the camera rig itself):
angleBetween = this.transform.eulerAngles.y - CameraRigTarget.transform.eulerAngles.y;
NewAngle = angle - angleBetween;
We then rotate the CameraRigTarget and the character based on this (AimX is the x value of the camera control input, as this is also where we evaluate the X input and move the camera around the player):
CameraRigTarget_Move.transform.RotateAround(this.transform.position, Vector3.up, (360f - NewAngle) + (AimX * LookSensHoriz * Time.deltaTime));
this.transform.Rotate(0, NewAngle, 0);
Alternatively, if you are looking to make the character look where the camera is looking right now, all you need is two of those lines:
angleBetween = this.transform.eulerAngles.y - CameraRigTarget.transform.eulerAngles.y;
this.transform.Rotate(0, angleBetween, 0);