38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using UnityEngine;
|
|
|
|
namespace Player
|
|
{
|
|
public class CameraController : MonoBehaviour
|
|
{
|
|
public Transform targetCameraPosition;
|
|
public float mouseSensitivity = 100f;
|
|
|
|
private Camera _camera;
|
|
private Transform _cameraTransform;
|
|
private float _xRotation = 0f;
|
|
|
|
void Start()
|
|
{
|
|
_camera = Camera.main;
|
|
if (_camera != null) _cameraTransform = _camera.transform;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
|
|
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
|
|
|
|
_xRotation -= mouseY;
|
|
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f);
|
|
|
|
if (Cursor.lockState == CursorLockMode.Locked)
|
|
{
|
|
transform.Rotate(Vector3.up * mouseX);
|
|
_cameraTransform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
|
|
_cameraTransform.rotation = Quaternion.Euler(_xRotation, transform.eulerAngles.y, 0f);
|
|
_cameraTransform.position = targetCameraPosition.position;
|
|
}
|
|
}
|
|
}
|
|
} |