Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- export var linear_velocity = Vector3.ZERO
- var velocity = Vector3.ZERO
- var jump = 5
- var accel = ground_acceleration
- var deaccel = ground_deceleration
- var speed = move_speed
- var gravity = 10
- export var jump_height : float = 10
- export var jump_time_to_peak : float = 0.5
- export var jump_time_to_descent : float = 0.1
- onready var jump_gravity :float = (-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)
- onready var jump_velocity :float = (2.0 * jump_height) / jump_time_to_peak
- onready var fall_gravity : float = (-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)
- export var move_speed := 15
- export var ground_acceleration := 15
- export var ground_deceleration := 15
- export var air_acceleration := 30
- export var air_deceleration := 0
- export var air_speed = 100
- export var mouse_sens = 0.2
- onready var look_pivot : Spatial = $Head
- var gravity_local: Vector3 = Vector3()
- func _input(event):
- if event is InputEventMouseMotion:
- rotate_y(deg2rad(-1 * event.relative.x) * mouse_sens)
- look_pivot.rotate_x(deg2rad(-1 * event.relative.y) * mouse_sens )
- look_pivot.rotation.x = clamp(look_pivot.rotation.x, deg2rad(-90), deg2rad(90))
- func _physics_process(delta):
- var move_direction := Vector3(0, 0, 0)
- if not is_on_floor():
- gravity_local += gravity * Vector3.DOWN * delta
- else:
- gravity_local = gravity * -get_floor_normal() * delta
- if Input.is_action_just_pressed("jump") and is_on_floor():
- gravity_local.y = jump
- if Input.is_action_pressed("move_forward"):
- move_direction.z -= Input.get_action_strength("move_forward")
- if Input.is_action_pressed("move_backward"):
- move_direction.z += Input.get_action_strength("move_backward")
- if Input.is_action_pressed("move_left"):
- move_direction.x -= Input.get_action_strength("move_left")
- if Input.is_action_pressed("move_right"):
- move_direction.x += Input.get_action_strength("move_right")
- move_direction = move_direction.normalized()
- move_direction = move_direction.rotated(Vector3.UP, rotation.y)
- if is_on_floor():
- accel = ground_acceleration
- deaccel = ground_deceleration
- speed = move_speed
- else:
- accel = air_acceleration
- deaccel = air_deceleration
- speed = air_speed
- # Ground movement
- if move_direction.length() > 0:
- linear_velocity = linear_velocity.linear_interpolate(move_direction * move_speed, accel * delta)
- else:
- linear_velocity = linear_velocity.linear_interpolate(Vector3.ZERO, deaccel * delta)
- linear_velocity = move_and_slide(linear_velocity + gravity_local, Vector3.UP)
- func get_gravity() -> float:
- return jump_gravity if velocity.y < 0.0 else fall_gravity
- func jump():
- linear_velocity.y = jump_velocity
- # Called when the node enters the scene tree for the first time.
- func _ready():
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Replace with function body.
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- #func _process(delta):
- # pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement