-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerLook.cs
55 lines (48 loc) · 1.42 KB
/
PlayerLook.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour {
[SerializeField] private string mouseXInputName, mouseYInputName;
[SerializeField] private float mouseSensitivity;
[SerializeField] private Transform playerBody;
private float xAxisClamp;
private void Awake()
{
LockCursor();
xAxisClamp = 0.0f;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraRotation();
}
private void CameraRotation()
{
float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
xAxisClamp += mouseY;
if (xAxisClamp > 90.0f)
{
xAxisClamp = 90.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(270.0f);
}
else if (xAxisClamp < -90.0f)
{
xAxisClamp = -90.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(90.0f);
}
transform.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
}
private void ClampXAxisRotationToValue(float value)
{
Vector3 eulerRotation = transform.eulerAngles;
eulerRotation.x = value;
transform.eulerAngles = eulerRotation;
}
}