4 Commits
2.5.1 ... 2.5.2

Author SHA1 Message Date
a8d151d568 Add controller support to PR #22 2024-07-15 15:10:44 -07:00
b458cba1a1 Rename handle_rotation to handle_head_rotation 2024-07-15 15:10:20 -07:00
Zakarya
4782f80ec2 Merge pull request #22 from Knockturnal/main
Fixed camera jitter
2024-07-15 14:52:49 -07:00
Knockturnal
8a398bade0 Fixed camera jitter 2024-07-15 17:24:58 +02:00
2 changed files with 23 additions and 15 deletions

View File

@ -33,7 +33,7 @@ You can make this a super basic controller by just disabling everything.
- In the controls export group, there is a commented section at the end that says "Uncomment this if you want full controller support". Uncomment that block.
- Make a key map for each direction (left, right, up, down) and map them to your joystick.
- Write in these keymaps in the controls section of the player settings.
- In the `_process` function, there is another block of commented code at the end that says the same thing. Uncomment that too.
- In the `handle_head_rotation` function, there is another block of commented code that says the same thing. Uncomment that too.
- You should now be able to look around with the joystick. Make sure you add the other controls to the input map. (movement, jumping, crouching, sprinting, etc.)
**How to change settings:**

View File

@ -40,10 +40,10 @@ extends CharacterBody3D
@export var SPRINT : String = "sprint"
# Uncomment if you want full controller support
#@export var LOOK_LEFT : String
#@export var LOOK_RIGHT : String
#@export var LOOK_UP : String
#@export var LOOK_DOWN : String
#@export var LOOK_LEFT : String = "look_left"
#@export var LOOK_RIGHT : String = "look_right"
#@export var LOOK_UP : String = "look_up"
#@export var LOOK_DOWN : String = "look_down"
@export_group("Feature Settings")
@export var jumping_enabled : bool = true
@ -75,6 +75,8 @@ var RETICLE : Control
# Get the gravity from the project settings to be synced with RigidBody nodes
var gravity : float = ProjectSettings.get_setting("physics/3d/default_gravity") # Don't set this as a const, see the gravity section in _physics_process
# Stores mouse input for rotating the camera in the phyhsics process
var mouseInput : Vector2 = Vector2(0,0)
func _ready():
#It is safe to comment this line if your game doesn't start with the mouse captured
@ -157,6 +159,8 @@ func _physics_process(delta):
if !immobile: # Immobility works by interrupting user input, so other forces can still be applied to the player
input_dir = Input.get_vector(LEFT, RIGHT, FORWARD, BACKWARD)
handle_movement(delta, input_dir)
handle_head_rotation()
# The player is not able to stand up if the ceiling is too low
low_ceiling = $CrouchCeilingDetection.is_colliding()
@ -214,6 +218,18 @@ func handle_movement(delta, input_dir):
velocity.x = direction.x * speed
velocity.z = direction.z * speed
func handle_head_rotation():
HEAD.rotation_degrees.y -= mouseInput.x * mouse_sensitivity
HEAD.rotation_degrees.x -= mouseInput.y * mouse_sensitivity
# Uncomment for controller support
#var controller_view_rotation = Input.get_vector(LOOK_DOWN, LOOK_UP, LOOK_RIGHT, LOOK_LEFT) * 0.035 # These are inverted because of the nature of 3D rotation.
#HEAD.rotation.x += controller_view_rotation.x
#HEAD.rotation.y += controller_view_rotation.y
mouseInput = Vector2(0,0)
HEAD.rotation.x = clamp(HEAD.rotation.x, deg_to_rad(-90), deg_to_rad(90))
func handle_state(moving):
if sprint_enabled:
@ -333,17 +349,9 @@ func _process(delta):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
Input.MOUSE_MODE_VISIBLE:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
HEAD.rotation.x = clamp(HEAD.rotation.x, deg_to_rad(-90), deg_to_rad(90))
# Uncomment if you want full controller support
#var controller_view_rotation = Input.get_vector(LOOK_LEFT, LOOK_RIGHT, LOOK_UP, LOOK_DOWN)
#HEAD.rotation_degrees.y -= controller_view_rotation.x * 1.5
#HEAD.rotation_degrees.x -= controller_view_rotation.y * 1.5
func _unhandled_input(event):
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
HEAD.rotation_degrees.y -= event.relative.x * mouse_sensitivity
HEAD.rotation_degrees.x -= event.relative.y * mouse_sensitivity
mouseInput.x += event.relative.x
mouseInput.y += event.relative.y