Skip to content

Instantly share code, notes, and snippets.

@jedStevens
Created July 31, 2018 15:09
Show Gist options
  • Save jedStevens/b9451234accc5b524db525b56506f56f to your computer and use it in GitHub Desktop.
Save jedStevens/b9451234accc5b524db525b56506f56f to your computer and use it in GitHub Desktop.
extends KinematicBody
var FOWARD = 200
var MOVEMENT = 200
var JUMP = Vector3(0,100,0)
var RUN_SPEED = 300
var mouse_threshold = 10
var mouse_sens_dampened = 0.01
var mouse_sens = 0.01
var mouse_acceleration = 0.1
var rot_vel = 0.0
var mouse_motion = Vector2()
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
set_process_input(true)
func _input(event):
if event is InputEventMouseMotion:
mouse_motion += event.relative
func _physics_process(delta):
#Close window/ end test.
if Input.is_key_pressed(KEY_ESCAPE):
get_tree().quit()
#Gravity and slope.
move_and_slide(Vector3(0,-6,0),Vector3(0,1,0))
#Jump.
if is_on_floor() and Input.is_key_pressed(KEY_SPACE):
move_and_slide(JUMP)
#Get viewport.
var viewcoords = get_viewport().size.floor()
var center_x = viewcoords.x / 2
var center_y = viewcoords.y / 2
var center = viewcoords / 2
var mouse_pos = get_viewport().get_mouse_position().floor()
#Warp mouse to center.
get_viewport().warp_mouse(center)
#Mouselook on X axis.
var vel = mouse_pos.floor() - center.floor()
if vel.length() > 0:
print(center, " | ", mouse_pos, " | ", vel)
if mouse_motion.x < 0:
$char.rotate_y(mouse_sens_dampened + delta)
if mouse_motion.x <= mouse_threshold:
$char.rotate_y(mouse_sens + delta)
if mouse_motion.x <= mouse_threshold * 2:
$char.rotate_y(mouse_acceleration + delta)
if mouse_motion.x > 0:
$char.rotate_y(-mouse_sens_dampened - delta)
if mouse_motion.x >= -mouse_threshold:
$char.rotate_y(-mouse_sens - delta)
if mouse_motion.x >= -mouse_threshold * 2:
$char.rotate_y(-mouse_acceleration - delta)
mouse_motion = Vector2()
#Local move and run.
if Input.is_key_pressed(KEY_SHIFT) and Input.is_key_pressed(KEY_W):
move_and_slide($char.transform.basis.z * delta * RUN_SPEED)
if Input.is_key_pressed(KEY_W):
move_and_slide(-$char.transform.basis.z * delta * FOWARD)
if Input.is_key_pressed(KEY_A):
move_and_slide($char.transform.basis.x * delta * MOVEMENT)
if Input.is_key_pressed(KEY_S):
move_and_slide($char.transform.basis.z * delta * MOVEMENT)
if Input.is_key_pressed(KEY_D):
move_and_slide($char.transform.basis.x * delta * -MOVEMENT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment