Download Assets/Scripts/PlayerCamController.cs from AliAch/my-unity-assets: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/datasets/AliAch/my-unity-assets/resolve/main/Assets/Scripts/PlayerCamController.cs
- Command line
-
hf download hf://datasets/AliAch/my-unity-assets/Assets/Scripts/PlayerCamController.cs
-
curl -L -o PlayerCamController.cs https://huggingface.co/datasets/AliAch/my-unity-assets/resolve/main/Assets/Scripts/PlayerCamController.cs
2.78 kB
| using UnityEngine; | |
| public class PlayerCamController : MonoBehaviour | |
| { | |
| [] | |
| [] private float sensX = 400f; | |
| [] private float sensY = 400f; | |
| [] | |
| [] private Transform playerOrient; | |
| [] private Transform camHolder; | |
| [] | |
| [] private PlayerMovement playerMovement; | |
| [] | |
| [] | |
| [] 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; | |
| } | |
| } |