Skip to content

Instantly share code, notes, and snippets.

@Efimero
Created August 6, 2020 20:31
Show Gist options
  • Save Efimero/05daf867dcc48656816d699e2579c8ba to your computer and use it in GitHub Desktop.
Save Efimero/05daf867dcc48656816d699e2579c8ba to your computer and use it in GitHub Desktop.
simple physics-controlled player script
extends RigidBody
# Called when the node enters the scene tree for the first time.
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
process_input(delta)
export var SPEED = 3000
export var JUMP = 300
var grounded = false
var allow_jump = true
func process_input(delta):
var r = get_node("Rotator").rotation.y
var s = 0
if grounded:
s = SPEED
else:
s = SPEED / 5
if self.linear_velocity.length_squared() < 15:
var f = Vector3.ZERO
if Input.is_action_pressed("forward"):
f += Vector3(0, 0, -1).rotated(Vector3(0, 1, 0), r)
if Input.is_action_pressed("backward"):
f += Vector3(0, 0, 1).rotated(Vector3(0, 1, 0), r)
if Input.is_action_pressed("leftward"):
f += Vector3(-1, 0, 0).rotated(Vector3(0, 1, 0), r)
if Input.is_action_pressed("rightward"):
f += Vector3(1, 0, 0).rotated(Vector3(0, 1, 0), r)
self.add_central_force(f.normalized() * s * delta)
if allow_jump and grounded and Input.is_action_pressed("jump"):
grounded = false
allow_jump = false
self.physics_material_override.friction = 0
self.apply_central_impulse(Vector3(0, JUMP * delta, 0))
if !Input.is_action_pressed("jump"):
allow_jump = true
func _input(event):
if event is InputEventKey and event.is_pressed():
if event.get_scancode() == KEY_ESCAPE:
get_tree().quit()
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
get_node("Rotator").rotate_y(deg2rad(-event.relative.x))
get_node("Rotator/Camera").rotate_x(deg2rad(-event.relative.y))
get_node("Rotator/Camera").rotation.x = clamp(get_node("Rotator/Camera").rotation.x, -PI/2, PI/2)
func _on_foot_body_entered(body):
#print_debug(body == self)
if body != self:
self.physics_material_override.friction = 2
grounded = true
func _on_Player_body_entered(body):
if body != self && body != get_node("foot"):
#print_debug(body.get_class())
pass # Replace with function body.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment