11extends CharacterBody3D
22class_name Character
33
4- const NORMAL_SPEED = 5 .0
5- const SPRINT_SPEED = 9 .0
4+ const NORMAL_SPEED = 6 .0
5+ const SPRINT_SPEED = 10 .0
66const JUMP_VELOCITY = 10
77
8- var current_speed : float
8+ @onready var nickname = $ PlayerNick/Nickname as Label3D
99
1010@export_category ("Objects" )
1111@export var _body : Node3D = null
1212@export var _spring_arm_offset : Node3D = null
1313
14+ var _current_speed : float
15+ var _respawn_point = Vector3 (0 , 5 , 0 )
1416var gravity = ProjectSettings .get_setting ("physics/3d/default_gravity" )
1517
1618func _enter_tree ():
1719 set_multiplayer_authority (str (name ).to_int ())
1820 $ SpringArmOffset/SpringArm3D/Camera3D .current = is_multiplayer_authority ()
19-
21+
22+ func _ready ():
23+ if multiplayer .is_server ():
24+ $ SpringArmOffset/SpringArm3D/Camera3D .current = false
25+
26+ change_nick (Network .players [multiplayer .get_unique_id ()]["nick" ])
27+
2028func _physics_process (delta ):
2129 if not is_multiplayer_authority ():
2230 return
@@ -34,6 +42,7 @@ func _physics_process(delta):
3442 _move ()
3543 move_and_slide ()
3644 _body .animate (velocity )
45+ _check_fall_and_respawn ()
3746
3847func _move () -> void :
3948 var _input_direction : Vector2 = Vector2 .ZERO
@@ -53,18 +62,33 @@ func _move() -> void:
5362 _direction = _direction .rotated (Vector3 .UP , _spring_arm_offset .rotation .y )
5463
5564 if _direction :
56- velocity .x = _direction .x * current_speed
57- velocity .z = _direction .z * current_speed
65+ velocity .x = _direction .x * _current_speed
66+ velocity .z = _direction .z * _current_speed
5867 _body .apply_rotation (velocity )
5968 return
6069
61- velocity .x = move_toward (velocity .x , 0 , current_speed )
62- velocity .z = move_toward (velocity .z , 0 , current_speed )
70+ velocity .x = move_toward (velocity .x , 0 , _current_speed )
71+ velocity .z = move_toward (velocity .z , 0 , _current_speed )
6372
6473func is_running () -> bool :
6574 if Input .is_action_pressed ("shift" ):
66- current_speed = SPRINT_SPEED
75+ _current_speed = SPRINT_SPEED
6776 return true
6877 else :
69- current_speed = NORMAL_SPEED
78+ _current_speed = NORMAL_SPEED
7079 return false
80+
81+ func _check_fall_and_respawn ():
82+ if global_transform .origin .y < - 15.0 :
83+ _respawn ()
84+
85+ func _respawn ():
86+ global_transform .origin = _respawn_point
87+ velocity = Vector3 .ZERO
88+
89+ @rpc ("any_peer" , "reliable" )
90+ func change_nick (new_nick : String ):
91+ if nickname :
92+ nickname .text = new_nick
93+ else :
94+ print ("error: nickname is null" )
0 commit comments