46 lines
1.4 KiB
GDScript
46 lines
1.4 KiB
GDScript
class_name Player
|
|
extends CharacterBody3D
|
|
|
|
@export var player_id := 1
|
|
@export var gravity_vector := Vector3(0, -20, 0)
|
|
@export var move_speed := 8.0
|
|
@export var accel := 50.0
|
|
@export var friction := 16.0
|
|
@export var jump_speed := 12.0
|
|
@export var air_control := 0.65
|
|
@export var coyote := 0.1
|
|
@export var buffer := 0.1
|
|
@export var col1 := Color(0.3, 0.6, 1)
|
|
@export var col2 := Color(1, 0.47, 0.35)
|
|
|
|
var ct := 0.0
|
|
var bt := 0.0
|
|
|
|
func _ready():
|
|
add_to_group("players")
|
|
# $Body.color = col1 if player_id == 1 else col2
|
|
|
|
func _physics_process(d):
|
|
var up = Vector3.UP if gravity_vector.length_squared() < 0.0001 else -gravity_vector.normalized()
|
|
up_direction = up
|
|
velocity += gravity_vector * d
|
|
ct = coyote if is_on_floor() else max(ct - d, 0.0)
|
|
bt = max(bt - d, 0.0)
|
|
if Input.is_action_just_pressed("p%d_jump" % player_id):
|
|
bt = buffer
|
|
var ix = Input.get_axis("p%d_left" % player_id, "p%d_right" % player_id)
|
|
var a = accel if is_on_floor() else accel * air_control
|
|
if abs(ix) > 0.01:
|
|
velocity.x = move_toward(velocity.x, ix * move_speed, a * d)
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0.0, friction * d)
|
|
if bt > 0.0 and ct > 0.0:
|
|
velocity = velocity.slide(up) + up * jump_speed
|
|
bt = 0.0
|
|
ct = 0.0
|
|
# plane lock: 2D platformer physics in a 3D scene. Sliding along an angled
|
|
# collision normal can otherwise walk the player off the Z=0 plane.
|
|
velocity.z = 0.0
|
|
move_and_slide()
|
|
global_position.z = 0.0
|