Skip to content

Commit 434dbeb

Browse files
committed
View bobbing abruptly moving to default camera pos fixed.
1 parent 4ee27d8 commit 434dbeb

File tree

5 files changed

+56
-56
lines changed

5 files changed

+56
-56
lines changed

Game Assets/Player/3DPlayer.tscn

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
[gd_scene load_steps=7 format=3 uid="uid://bsljbif1rji7e"]
1+
[gd_scene load_steps=5 format=3 uid="uid://bsljbif1rji7e"]
22

33
[ext_resource type="Script" path="res://Game Assets/Player/3DPlayer_SCRIPT.gd" id="1_dbfha"]
4-
[ext_resource type="Script" path="res://Textures/Icons/Script Icons/Scripts/CameraIcon_SCRIPT.gd" id="2_ef18p"]
54
[ext_resource type="Texture2D" uid="uid://tmqrfb1atg1j" path="res://Textures/Crosshairs/Expland_Crosshair.png" id="5_due8n"]
6-
[ext_resource type="Script" path="res://Scenes and Scripts/Scenes/Player/CrosshairCanvas/Crosshair_SCRIPT.gd" id="6_01j83"]
75

86
[sub_resource type="CapsuleMesh" id="CapsuleMesh_e8ep2"]
97

108
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_ocuv0"]
119
points = PackedVector3Array(-0.125207, -0.532801, -0.480507, 0.0227831, 0.47607, 0.498884, 0.169713, 0.559144, 0.464172, 0.231051, -0.803591, 0.320455, 0.40741, 0.651043, -0.243523, -0.482789, 0.594843, 0.0822132, -0.362868, -0.682312, 0.289697, 0.469044, -0.654529, -0.0662713, -0.127444, 0.842701, -0.338103, -0.393435, -0.683942, -0.244717, 0.438255, 0.623309, 0.200849, 0.0841477, 0.977454, 0.114795, -0.0682023, -0.976458, -0.12927, 0.20055, -0.563129, -0.451454, -0.185527, 0.595453, -0.453475, -0.273363, 0.592268, 0.407754, -0.00693649, -0.476823, 0.49966, 0.375821, -0.588614, 0.316955, 0.111579, 0.563059, -0.481177, -0.41725, 0.527866, -0.270497, -0.484546, -0.596972, -0.0665097, -0.279747, 0.908561, 0.0533361, -0.250197, -0.880712, 0.205319, 0.263647, -0.902771, -0.127394, 0.293368, 0.871526, -0.157196, 0.373412, -0.526319, -0.328246, 0.499663, 0.476641, -0.00688856, 0.0531056, 0.875001, 0.324703, -0.154543, -0.590854, 0.465879, -0.0972799, -0.782358, -0.398188, -0.387649, -0.498171, 0.31565, -0.30068, -0.587995, -0.388901)
1210

13-
[node name="Player" type="CharacterBody3D"]
11+
[node name="3DPlayer" type="CharacterBody3D"]
1412
script = ExtResource("1_dbfha")
1513

16-
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
14+
[node name="Body" type="MeshInstance3D" parent="."]
1715
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
1816
mesh = SubResource("CapsuleMesh_e8ep2")
1917

@@ -25,7 +23,6 @@ shape = SubResource("ConvexPolygonShape3D_ocuv0")
2523
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.66439, 0)
2624

2725
[node name="Camera3D" type="Camera3D" parent="Head"]
28-
script = ExtResource("2_ef18p")
2926

3027
[node name="CrosshairCanvas" type="CanvasLayer" parent="Head/Camera3D"]
3128

@@ -40,5 +37,4 @@ grow_horizontal = 2
4037
grow_vertical = 2
4138
texture = ExtResource("5_due8n")
4239
expand_mode = 1
43-
script = ExtResource("6_01j83")
4440
metadata/_edit_use_anchors_ = true

Game Assets/Player/3DPlayer_SCRIPT.gd

+25-24
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extends CharacterBody3D # Inheritance
4040

4141
@export var BOB_FREQ := 3.0 # The frequency of the waves (how often it occurs)
4242
@export var BOB_AMP = 0.08 # The amplitude of the waves (how much you actually go up and down)
43+
@export var BOB_SMOOTHING_SPEED := 3.0 # Speed to smooth the return to the original position. The lower it get's, the smoother it is.
4344

4445
@export_subgroup("Other") # a subgroup for other view bobbing variables.
4546
@export var Wave_Length = 0.0 # The wavelength of the bobbing
@@ -82,6 +83,7 @@ func _unhandled_input(event):
8283
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
8384
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
8485
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
86+
# Modify _physics_process to include smooth transition when not moving
8587
func _physics_process(delta):
8688
# Always apply gravity regardless of the game state
8789
if not is_on_floor():
@@ -90,51 +92,50 @@ func _physics_process(delta):
9092
# Jumping
9193
if Input.is_action_just_pressed("Jump") and is_on_floor():
9294
velocity.y = JUMP_VELOCITY
93-
95+
9496
# Sprinting
95-
if Input.is_action_pressed("Sprint"):
96-
speed = SPRINT_SPEED
97-
else:
98-
speed = WALK_SPEED
99-
97+
speed = SPRINT_SPEED if Input.is_action_pressed("Sprint") else WALK_SPEED
98+
10099
# Movement
101100
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
102101
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
102+
103103
if is_on_floor():
104-
if direction:
104+
if direction != Vector3.ZERO:
105105
velocity.x = direction.x * speed
106106
velocity.z = direction.z * speed
107107
else:
108-
velocity.x = lerp(velocity.x, float(direction.x) * speed, delta * 10.0)
109-
velocity.z = lerp(velocity.z, float(direction.z) * speed, delta * 10.0)
108+
velocity.x = lerp(velocity.x, 0.0, delta * 10.0)
109+
velocity.z = lerp(velocity.z, 0.0, delta * 10.0)
110110
else:
111-
velocity.x = lerp(velocity.x, float(direction.x) * speed, delta * 3.0)
112-
velocity.z = lerp(velocity.z, float(direction.z) * speed, delta * 3.0)
113-
114-
111+
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
112+
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
113+
115114
move_and_slide()
116-
# Check if the player is moving (i.e., not just stopped by a collision) and on the floor
115+
116+
# Check if the player is moving and on the floor
117117
var is_moving = velocity.length() > 0.1 and is_on_floor()
118-
118+
119119
# Apply view bobbing only if the player is moving
120120
if is_moving:
121121
Wave_Length += delta * velocity.length()
122122
camera.transform.origin = _headbob(Wave_Length)
123123
else:
124-
camera.transform.origin.y = 0 # Reset bobbing when not moving
124+
# Smoothly return to original position when not moving
125+
var target_pos = Vector3(camera.transform.origin.x, 0, camera.transform.origin.z)
126+
camera.transform.origin = camera.transform.origin.lerp(target_pos, delta * BOB_SMOOTHING_SPEED)
125127

126-
func _process(_delta):
127-
128-
if FOV == 9999:
129-
$Head/Camera3D.fov = PlayerSettingsData.FOV
130-
else:
131-
$Head/Camera3D.fov = FOV
132-
133-
$Head/Camera3D/CrosshairCanvas/Crosshair.size = crosshair_size
128+
# The headbob function remains the same
134129
func _headbob(time) -> Vector3:
135130
var pos = Vector3.ZERO
136131
pos.y = sin(time * BOB_FREQ) * BOB_AMP
137132
return pos
133+
func _process(_delta):
134+
135+
$Head/Camera3D.fov = FOV
136+
137+
$Head/Camera3D/CrosshairCanvas/Crosshair.size = crosshair_size
138+
138139

139140
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
140141
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

Scenes and Scripts/Scenes/Player/Player_SCRIPT.gd

+24-21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The keyword @export means that they can be accessed in the inspector panel (righ
5151

5252
@export var BOB_FREQ := 3.0 # The frequency of the waves (how often it occurs)
5353
@export var BOB_AMP = 0.08 # The amplitude of the waves (how much you actually go up and down)
54+
@export var BOB_SMOOTHING_SPEED := 3.0 # Speed to smooth the return to the original position. The lower it get's, the smoother it is.
5455

5556
@export_subgroup("Other") # a subgroup for other view bobbing variables.
5657
@export var Wave_Length = 0.0 # The wavelength of the bobbing
@@ -174,7 +175,6 @@ func _physics_process(delta):
174175

175176
# Handle movement restrictions when the inventory is open
176177
if GAME_STATE == "INVENTORY":
177-
# If the player is on the ground when the inventory is opened or lands, stop movement
178178
if is_on_floor():
179179
velocity.x = 0
180180
velocity.z = 0
@@ -185,38 +185,44 @@ func _physics_process(delta):
185185
# Jumping
186186
if Input.is_action_just_pressed("Jump") and is_on_floor():
187187
velocity.y = JUMP_VELOCITY
188-
188+
189189
# Sprinting
190-
if Input.is_action_pressed("Sprint"):
191-
speed = SPRINT_SPEED
192-
else:
193-
speed = WALK_SPEED
194-
190+
speed = SPRINT_SPEED if Input.is_action_pressed("Sprint") else WALK_SPEED
191+
195192
# Movement
196193
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
197194
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
195+
198196
if is_on_floor():
199-
if direction:
197+
if direction != Vector3.ZERO:
200198
velocity.x = direction.x * speed
201199
velocity.z = direction.z * speed
202200
else:
203-
velocity.x = lerp(velocity.x, float(direction.x) * speed, delta * 10.0)
204-
velocity.z = lerp(velocity.z, float(direction.z) * speed, delta * 10.0)
201+
velocity.x = lerp(velocity.x, 0.0, delta * 10.0)
202+
velocity.z = lerp(velocity.z, 0.0, delta * 10.0)
205203
else:
206-
velocity.x = lerp(velocity.x, float(direction.x) * speed, delta * 3.0)
207-
velocity.z = lerp(velocity.z, float(direction.z) * speed, delta * 3.0)
208-
209-
204+
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
205+
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
206+
210207
move_and_slide()
211-
# Check if the player is moving (i.e., not just stopped by a collision) and on the floor
208+
209+
# Check if the player is moving and on the floor
212210
var is_moving = velocity.length() > 0.1 and is_on_floor()
213-
211+
214212
# Apply view bobbing only if the player is moving
215213
if is_moving:
216214
Wave_Length += delta * velocity.length()
217215
camera.transform.origin = _headbob(Wave_Length)
218216
else:
219-
camera.transform.origin.y = 0 # Reset bobbing when not moving
217+
# Smoothly return to original position when not moving
218+
var target_pos = Vector3(camera.transform.origin.x, 0, camera.transform.origin.z)
219+
camera.transform.origin = camera.transform.origin.lerp(target_pos, delta * BOB_SMOOTHING_SPEED)
220+
221+
# The headbob function remains the same
222+
func _headbob(time) -> Vector3:
223+
var pos = Vector3.ZERO
224+
pos.y = sin(time * BOB_FREQ) * BOB_AMP
225+
return pos
220226
func _process(_delta):
221227

222228
if UseHealth == false:
@@ -234,10 +240,7 @@ func _process(_delta):
234240
$Head/Camera3D.fov = FOV
235241

236242
$Head/Camera3D/CrosshairCanvas/Crosshair.size = crosshair_size
237-
func _headbob(time) -> Vector3:
238-
var pos = Vector3.ZERO
239-
pos.y = sin(time * BOB_FREQ) * BOB_AMP
240-
return pos
243+
241244

242245
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
243246
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

Scenes and Scripts/Scenes/Tests/test.tscn

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_scene load_steps=5 format=3 uid="uid://clqkg5xb8eu01"]
22

3-
[ext_resource type="PackedScene" uid="uid://bsljbif1rji7e" path="res://Game Assets/Player/3DPlayer.tscn" id="1_5eu0s"]
3+
[ext_resource type="PackedScene" uid="uid://dlbkch6iuy8gn" path="res://Godot_First-Person-Controller-7310c731306fc98c7209fb28666ea85523cf86f5/addons/fp_controller/player.tscn" id="1_86eax"]
44

55
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_csbo8"]
66
sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1)
@@ -29,5 +29,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0912914, 0.0251261, -1.6345
2929
use_collision = true
3030
size = Vector3(17.249, 1, 21.6737)
3131

32-
[node name="Player" parent="." instance=ExtResource("1_5eu0s")]
33-
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.805227, 0.602243, 0.472525)
32+
[node name="Player" parent="." instance=ExtResource("1_86eax")]
33+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)

player.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"GAME_STATE":"NORMAL","Health":100,"Position":{"x":-34.737117767334,"y":24.3865070343018,"z":29.1238059997559}}
1+
{"GAME_STATE":"NORMAL","Health":100,"Position":{"x":-22.5039043426514,"y":24.8419246673584,"z":11.2414655685425}}

0 commit comments

Comments
 (0)