Last active
May 3, 2023 20:35
-
-
Save ynot01/1fcaeb611fe2894378dd98ca995b3c1a to your computer and use it in GitHub Desktop.
Godot 3D First Person Camera Rotation
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
| # MIT License | |
| # | |
| # Copyright (c) 2023 ynot01 | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| extends Camera3D | |
| var SENSITIVITY = 1.0 | |
| var LOOK_DOWN_LIMIT = -PI/2 | |
| var LOOK_UP_LIMIT = PI/2 | |
| var paused = false | |
| func _ready(): | |
| DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED) | |
| func _input(event): | |
| if event is InputEventMouseMotion and DisplayServer.mouse_get_mode() == DisplayServer.MOUSE_MODE_CAPTURED: | |
| # Rotate the camera up and down | |
| rotation.x = clamp(rotation.x - event.relative.y / (SENSITIVITY * 400.0), LOOK_DOWN_LIMIT, LOOK_UP_LIMIT) | |
| # Rotate the camera horizontally | |
| rotation.y -= event.relative.x / (SENSITIVITY * 400.0) | |
| # Assumes that you have an input action called "menu" | |
| if event.is_action_pressed("menu"): | |
| cycle_pause() # Frees the mouse to close the window or do other things | |
| func cycle_pause(): | |
| if paused: | |
| unpause() | |
| else: | |
| pause() | |
| func pause(): | |
| DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_VISIBLE) | |
| paused = true | |
| func unpause(): | |
| DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED) | |
| paused = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment