Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- const GRAVITY = -50
- var vel = Vector3()
- const MAX_SPEED = 10
- const JUMP_SPEED = 25
- const ACCEL= 5
- var in_air = 0
- const SPRINT_SPEED = 20
- const SPRINT_ACCEL = 10
- var is_sprinting = false
- var dir = Vector3()
- ###DECEL will determine how fast do you come to a stop, both on flat and sloped surfaces###
- const DECEL= 15
- ######################################
- ###you need these 3###################
- const MAX_SLOPE_ANGLE = 45
- const FLOOR_NORMAL = Vector3(0, 1, 0)
- var _snap: Vector3
- ######################################
- var camera
- var rotation_helper
- var MOUSE_SENSITIVITY = 0.2
- func _ready():
- camera = $Spatial/Camera
- rotation_helper = $Spatial
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
- func _physics_process(delta):
- _process_input(delta)
- _process_movement(delta)
- func _process_input(delta):
- dir = Vector3()
- var cam_xform = camera.get_global_transform()
- var input_movement_vector = Vector2()
- if Input.is_action_pressed("movement_forward"):
- input_movement_vector.y += 1
- if Input.is_action_pressed("movement_backward"):
- input_movement_vector.y -= 1
- if Input.is_action_pressed("movement_left"):
- input_movement_vector.x -= 1
- if Input.is_action_pressed("movement_right"):
- input_movement_vector.x += 1
- input_movement_vector = input_movement_vector.normalized()
- dir += -cam_xform.basis.z.normalized() * input_movement_vector.y
- dir += cam_xform.basis.x.normalized() * input_movement_vector.x
- ###############################################################
- ############ if is_on_floor(): is crucial######################
- ###############################################################
- if is_on_floor():
- _snap = Vector3(0, -1, 0)
- if Input.is_action_just_pressed("movement_jump"):
- _snap = Vector3(0, 0, 0)
- vel.y = JUMP_SPEED
- ###############################################################
- if Input.is_action_pressed("movement_sprint"):
- is_sprinting = true
- else:
- is_sprinting = false
- func _process_movement(delta):
- dir.y = 0
- dir = dir.normalized()
- vel.y += delta*GRAVITY
- var hvel = vel
- hvel.y = 0
- var target = dir
- if is_sprinting:
- target *= SPRINT_SPEED
- else:
- target *= MAX_SPEED
- var accel
- if dir.dot(hvel) > 0:
- if is_sprinting:
- accel = SPRINT_ACCEL
- else:
- accel = ACCEL
- else:
- accel = DECEL
- hvel = hvel.linear_interpolate(target, accel*delta)
- vel.x = hvel.x
- vel.z = hvel.z
- ####################################################################################################
- #This line is very important, you need the .y otherwise you will be able to move but you will slide#
- ####################################################################################################
- vel.y = move_and_slide_with_snap(vel, _snap, FLOOR_NORMAL, true, 4, deg2rad(MAX_SLOPE_ANGLE)).y
- ####################################################################################################
- func _input(event):
- if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
- rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
- self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
- var camera_rot = rotation_helper.rotation_degrees
- camera_rot.x = clamp(camera_rot.x, -80, 80)
- rotation_helper.rotation_degrees = camera_rot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement