File size: 2,780 Bytes
62ad2a8
 
 
 
f069bd4
 
 
62ad2a8
f069bd4
 
72180aa
 
 
 
 
 
 
62ad2a8
f069bd4
 
 
72180aa
 
 
f069bd4
62ad2a8
 
f069bd4
12fea46
72180aa
 
12fea46
 
 
 
 
 
 
 
 
 
 
62ad2a8
 
f069bd4
62ad2a8
f069bd4
 
62ad2a8
 
 
 
 
72180aa
 
f069bd4
 
 
 
 
 
 
 
 
 
 
62ad2a8
72180aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using UnityEngine;

public class PlayerCamController : MonoBehaviour
{
    [Header("Sensitivity Settings")]
    [SerializeField] private float sensX = 400f;
    [SerializeField] private float sensY = 400f;

    [Header("References")]
    [SerializeField] private Transform playerOrient;
    [SerializeField] private Transform camHolder;
    [Tooltip("Used to detect wall-sticking so yaw can be clamped while stuck to a wall. Auto-found in scene if left empty.")]
    [SerializeField] private PlayerMovement playerMovement;

    [Header("Wall Stick Yaw Clamp")]
    [Tooltip("How far (degrees) the player can turn left/right of the yaw they were facing when they stuck to the wall.")]
    [SerializeField] private float wallStickYawClampRange = 200f;

    private float rotXCam;
    private float rotYCam;

    private bool wasWallSticking;
    private float wallStickYawAnchor;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;

        if (playerMovement == null) playerMovement = FindObjectOfType<PlayerMovement>();

        // Cache the current rotation angles on startup so no snap to 0
        if (camHolder != null)
        {
            Vector3 currentRotation = camHolder.eulerAngles;

            // Normalize angles 
            rotXCam = currentRotation.x;
            if (rotXCam > 180) rotXCam -= 360f;

            rotYCam = currentRotation.y;
        }
    }

    private void Update()
    {
        float mouseX = Input.GetAxisRaw("Mouse X") * sensX * Time.deltaTime;
        float mouseY = Input.GetAxisRaw("Mouse Y") * sensY * Time.deltaTime;

        rotYCam += mouseX;
        rotXCam -= mouseY;
        rotXCam = Mathf.Clamp(rotXCam, -80f, 80f);

        HandleWallStickYawClamp();

        // Rotate the HOLDER instead of the camera child to maintain the pivot
        if (camHolder != null)
        {
            camHolder.rotation = Quaternion.Euler(rotXCam, rotYCam, 0);
        }

        // Rotate player body orientation on Y axis
        if (playerOrient != null)
        {
            playerOrient.rotation = Quaternion.Euler(0, rotYCam, 0);
        }
    }

    private void HandleWallStickYawClamp()
    {
        if (playerMovement == null) return;

        bool isSticking = playerMovement.IsWallSticking;

        // Just started sticking this frame - anchor the clamp to the current facing.
        if (isSticking && !wasWallSticking)
        {
            wallStickYawAnchor = rotYCam;
        }

        if (isSticking)
        {
            float min = wallStickYawAnchor - wallStickYawClampRange;
            float max = wallStickYawAnchor + wallStickYawClampRange;
            rotYCam = Mathf.Clamp(rotYCam, min, max);
        }

        wasWallSticking = isSticking;
    }
}