-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerCameraRotation.cs
More file actions
67 lines (54 loc) · 1.87 KB
/
PlayerCameraRotation.cs
File metadata and controls
67 lines (54 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerCameraRotation : MonoBehaviour
{
[SerializeField] private float _sensitivity = 10f;
[SerializeField] private float _maxLookAngle = 80f;
private GameObject _player;
private InputSystem_Actions _inputSystem;
private Vector2 _lookRotation;
private float _xRotation = 0f;
private void Awake()
{
_player = GameObject.FindGameObjectWithTag("Player");
if (_player == null)
{
Debug.LogWarning("Player is not find!");
enabled = false;
return;
}
_inputSystem = new InputSystem_Actions();
}
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void OnEnable()
{
_inputSystem.Player.Enable();
_inputSystem.Player.Look.performed += LookRotation;
_inputSystem.Player.Look.canceled += LookRotation;
}
private void OnDisable()
{
_inputSystem.Player.Disable();
_inputSystem.Player.Look.performed -= LookRotation;
_inputSystem.Player.Look.canceled -= LookRotation;
}
public void LookRotation(InputAction.CallbackContext context)
{
_lookRotation = context.ReadValue<Vector2>();
}
private void LateUpdate()
{
if (_player == null) return;
transform.position = _player.transform.position + Vector3.up;
float mouseX = _lookRotation.x * _sensitivity * Time.deltaTime;
_player.transform.Rotate(Vector3.up, mouseX);
float mouseY = _lookRotation.y * _sensitivity * Time.deltaTime;
_xRotation -= mouseY;
_xRotation = Mathf.Clamp(_xRotation, -_maxLookAngle, _maxLookAngle);
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
}
}