Advertisement
Damo_108

Player_Template.gd

Feb 17th, 2020
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. const GRAVITY = -50
  4. var vel = Vector3()
  5. const MAX_SPEED = 10
  6. const JUMP_SPEED = 25
  7. const ACCEL= 5
  8. var in_air = 0
  9. const SPRINT_SPEED = 20
  10. const SPRINT_ACCEL = 10
  11. var is_sprinting = false
  12. var dir = Vector3()
  13.  
  14. ###DECEL will determine how fast do you come to a stop, both on flat and sloped surfaces###
  15. const DECEL= 15
  16.  
  17. ######################################
  18. ###you need these 3###################
  19. const MAX_SLOPE_ANGLE = 45
  20. const FLOOR_NORMAL = Vector3(0, 1, 0)
  21. var _snap: Vector3
  22. ######################################
  23.  
  24. var camera
  25. var rotation_helper
  26. var MOUSE_SENSITIVITY = 0.2
  27.  
  28. func _ready():
  29.     camera = $Spatial/Camera
  30.     rotation_helper = $Spatial
  31.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  32.  
  33. func _physics_process(delta):
  34.     _process_input(delta)
  35.     _process_movement(delta)
  36.  
  37. func _process_input(delta):
  38.    
  39.     dir = Vector3()
  40.    
  41.     var cam_xform = camera.get_global_transform()
  42.    
  43.     var input_movement_vector = Vector2()
  44.    
  45.     if Input.is_action_pressed("movement_forward"):
  46.         input_movement_vector.y += 1
  47.     if Input.is_action_pressed("movement_backward"):
  48.         input_movement_vector.y -= 1
  49.     if Input.is_action_pressed("movement_left"):
  50.         input_movement_vector.x -= 1
  51.     if Input.is_action_pressed("movement_right"):
  52.         input_movement_vector.x += 1
  53.    
  54.     input_movement_vector = input_movement_vector.normalized()
  55.    
  56.     dir += -cam_xform.basis.z.normalized() * input_movement_vector.y
  57.     dir += cam_xform.basis.x.normalized() * input_movement_vector.x
  58.    
  59.     ###############################################################
  60.     ############ if is_on_floor(): is crucial######################
  61.     ###############################################################
  62.     if is_on_floor():
  63.         _snap = Vector3(0, -1, 0)
  64.         if Input.is_action_just_pressed("movement_jump"):
  65.             _snap = Vector3(0, 0, 0)
  66.             vel.y = JUMP_SPEED
  67.     ###############################################################
  68.    
  69.     if Input.is_action_pressed("movement_sprint"):
  70.         is_sprinting = true
  71.     else:
  72.         is_sprinting = false
  73.  
  74. func _process_movement(delta):
  75.     dir.y = 0
  76.     dir = dir.normalized()
  77.    
  78.     vel.y += delta*GRAVITY
  79.    
  80.     var hvel = vel
  81.     hvel.y = 0
  82.    
  83.     var target = dir
  84.     if is_sprinting:
  85.         target *= SPRINT_SPEED
  86.     else:
  87.         target *= MAX_SPEED
  88.    
  89.     var accel
  90.     if dir.dot(hvel) > 0:
  91.         if is_sprinting:
  92.             accel = SPRINT_ACCEL
  93.         else:
  94.             accel = ACCEL
  95.     else:
  96.         accel = DECEL
  97.    
  98.     hvel = hvel.linear_interpolate(target, accel*delta)
  99.     vel.x = hvel.x
  100.     vel.z = hvel.z
  101.    
  102.     ####################################################################################################
  103.     #This line is very important, you need the .y otherwise you will be able to move but you will slide#
  104.     ####################################################################################################
  105.     vel.y = move_and_slide_with_snap(vel, _snap, FLOOR_NORMAL, true, 4, deg2rad(MAX_SLOPE_ANGLE)).y
  106.     ####################################################################################################
  107.  
  108.  
  109. func _input(event):
  110.     if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  111.         rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
  112.         self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
  113.        
  114.         var camera_rot = rotation_helper.rotation_degrees
  115.         camera_rot.x = clamp(camera_rot.x, -80, 80)
  116.         rotation_helper.rotation_degrees = camera_rot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement