Last active
August 5, 2025 10:07
-
-
Save aberloni/a18426c55d7a6d4c263712a0e173797b to your computer and use it in GitHub Desktop.
Yet another FPS controller simple script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.InputSystem; | |
| public class KappaFps : MonoBehaviour | |
| { | |
| /// <summary> | |
| /// buffer for delta positioning | |
| /// </summary> | |
| Vector2 frame_mouse_position; | |
| /// <summary> | |
| /// ref pivot for ZQSD movement in space | |
| /// body pivot, will rotate horizontally | |
| /// </summary> | |
| public Transform body => transform; | |
| /// <summary> | |
| /// head pivot, will rotate vertically | |
| /// </summary> | |
| public Transform head; | |
| public float move_speed = 1f; | |
| public float h_speed = 360f; | |
| public float v_speed = 180f; | |
| public float viewAngle = 60f; | |
| Keyboard Keyb => Keyboard.current; | |
| void Update() | |
| { | |
| if(Cursor.lockState == CursorLockMode.Locked) | |
| { | |
| update_movement(); | |
| update_looking(); | |
| } | |
| if (Cursor.lockState != CursorLockMode.Locked && Mouse.current.leftButton.wasPressedThisFrame) Cursor.lockState = CursorLockMode.Locked; | |
| else if (Cursor.lockState == CursorLockMode.Locked && Keyb.escapeKey.wasReleasedThisFrame) Cursor.lockState = CursorLockMode.None; | |
| } | |
| void update_looking() | |
| { | |
| Vector2 mouse = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); | |
| Vector2 delta = mouse - frame_mouse_position; | |
| if (delta.sqrMagnitude != 0f) | |
| { | |
| if (delta.x != 0f) | |
| { | |
| body.Rotate(Vector3.up, delta.x * h_speed * Time.deltaTime); | |
| } | |
| if (delta.y != 0f) | |
| { | |
| head.Rotate(Vector3.right, -delta.y * v_speed * Time.deltaTime); | |
| //Debug.Log(v.localEulerAngles); | |
| Vector3 local = head.localEulerAngles; | |
| if (local.x > viewAngle && local.x < 180f) | |
| { | |
| local.y = local.z = 0f; | |
| local.x = viewAngle; | |
| } | |
| if (local.x > 180f && local.x < 360f - viewAngle) | |
| { | |
| local.y = local.z = 0f; | |
| local.x = 360f - viewAngle; | |
| } | |
| head.localEulerAngles = local; | |
| } | |
| } | |
| } | |
| void update_movement() | |
| { | |
| Vector3 dir = Vector3.zero; | |
| if (Keyb.qKey.isPressed || Keyb.aKey.isPressed) // left | |
| { | |
| dir.x = -1f; | |
| } | |
| else if (Keyb.dKey.isPressed) // right | |
| { | |
| dir.x = 1f; | |
| } | |
| if (Keyb.zKey.isPressed || Keyb.wKey.isPressed) // forward | |
| { | |
| dir.z = 1f; | |
| } | |
| else if (Keyb.sKey.isPressed) // backward | |
| { | |
| dir.z = -1f; | |
| } | |
| body.Translate(dir * move_speed * Time.deltaTime, Space.Self); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment