- Home /
Question by
SizzlinSam · Jun 04, 2019 at 02:10 AM ·
c#camera3dwalking3rd person camera
How can I make the player walk forwards to where the camera is facing
I have a cube that can walk around with WASD and you can move the cursor around and see around you. This is 3rd person by the way. However, if you turn the camera one direction and press W that will still make you walk forwards from where the camera originated. I am hoping that someone has an idea for how I can make it so that if you turn the camera and press W it will take you forwards to where you are looking and the same for ASD where it will take you in the correct direction. If you do not understand me, I am basically just trying to make a basic 3rd person camera script. This is what the script looks like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 80.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
private float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private void Start()
{
camTransform = transform;
cam = Camera.main;
}
private void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position);
}
}
Comment