Guest User

Untitled

a guest
Sep 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3.  
  4. const GRAVITY = -40.8
  5. var vel = Vector3()
  6. const MAX_SPEED = 20
  7. const JUMP_SPEED = 18
  8. const ACCEL = 4.5
  9. const SPEED = 1
  10.  
  11. var dir = Vector3()
  12.  
  13. const DEACCEL= 16
  14. const MAX_SLOPE_ANGLE = 40
  15.  
  16. var camera
  17. var rotation_helper
  18.  
  19. var MOUSE_SENSITIVITY = 0.5
  20.  
  21. func _ready():
  22. camera = $rothelp/Camera
  23. rotation_helper = $rothelp
  24.  
  25. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  26.  
  27. func _physics_process(delta):
  28. process_input(delta)
  29. process_movement(delta)
  30.  
  31. func process_input(delta):
  32.  
  33. # ----------------------------------
  34. # Walking
  35. dir = Vector3()
  36. var cam_xform = camera.get_global_transform()
  37.  
  38. var input_movement_vector = Vector2()
  39.  
  40. if Input.is_action_pressed("ui_up"):
  41. input_movement_vector.y += SPEED
  42. if Input.is_action_pressed("ui_down"):
  43. input_movement_vector.y -= SPEED
  44. if Input.is_action_pressed("ui_left"):
  45. input_movement_vector.x -= SPEED
  46. if Input.is_action_pressed("ui_right"):
  47. input_movement_vector.x += SPEED
  48.  
  49. input_movement_vector = input_movement_vector.normalized()
  50.  
  51. # Basis vectors are already normalized.
  52. dir += -cam_xform.basis.z * input_movement_vector.y
  53. dir += cam_xform.basis.x * input_movement_vector.x
  54. # ----------------------------------
  55.  
  56. # ----------------------------------
  57. # Jumping
  58. if is_on_floor():
  59. if Input.is_action_just_pressed("ui_jump"):
  60. vel.y = JUMP_SPEED
  61. # ----------------------------------
  62.  
  63. # ----------------------------------
  64. # Capturing/Freeing the cursor
  65. if Input.is_action_just_pressed("ui_cancel"):
  66. if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
  67. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  68. else:
  69. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  70. # ----------------------------------
  71.  
  72. func process_movement(delta):
  73. dir.y = 0
  74. dir = dir.normalized()
  75.  
  76. vel.y += delta * GRAVITY
  77.  
  78. var hvel = vel
  79. hvel.y = 0
  80.  
  81. var target = dir
  82. target *= MAX_SPEED
  83.  
  84. var accel
  85. if dir.dot(hvel) > 0:
  86. accel = ACCEL
  87. else:
  88. accel = DEACCEL
  89.  
  90. hvel = hvel.linear_interpolate(target, accel * delta)
  91. vel.x = hvel.x
  92. vel.z = hvel.z
  93. vel = move_and_slide(vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
  94.  
  95. func _input(event):
  96. if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  97. rotation_helper.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
  98. self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
  99.  
  100.  
  101. var camera_rot = rotation_helper.rotation_degrees
  102. camera_rot.x = clamp(camera_rot.x, -89, 89)
  103. rotation_helper.rotation_degrees = camera_rot
Advertisement
Add Comment
Please, Sign In to add comment