Hello guys,
I have watched some tutorials and make my object - player move with a keyboard and camera which is orbiting around that player via mouse controller but the main problem is that I cant make player move the direction of my camera looking.
THIS IS MY PLAYER
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
public float speed = 2.0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(moveHorizontal, 0.0f, moveVertical) * speed);
}
}
THIS IS MY CAMERA
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
{
// Start is called before the first frame update
private const float Y_ANGLE_MIN = -50.0f;
private const float Y_ANGLE_MAX = 50.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 sensivityX = 4.0f;
private float sensivityY = 1.0f;
private Vector3 movement;
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);
Vector3 relativeMovement = Camera.main.transform.TransformVector(movement);
}
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);
}
}