Player.gd 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. extends KinematicBody
  2. # physics
  3. var moveSpeed : float = 5.0
  4. # cam look
  5. var minLookAngle : float = -90.0
  6. var maxLookAngle : float = 90.0
  7. var lookSensitivity : float = 10.0
  8. # vectors
  9. var vel : Vector3 = Vector3()
  10. var mouseDelta : Vector2 = Vector2()
  11. onready var camera : Camera = get_node("Camera")
  12. func _ready():
  13. move_and_slide(Vector3(60.503445, 30.231335, 1734.286987) + Vector3(0,0,10), Vector3.UP)
  14. func _process(delta):
  15. # rotate the camera along the x axis
  16. camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta
  17. # clamp camera x rotation axis
  18. camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)
  19. # rotate the player along their y-axis
  20. rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta
  21. # reset the mouseDelta vector
  22. mouseDelta = Vector2()
  23. func _input(event):
  24. if event is InputEventMouseMotion:
  25. mouseDelta = event.relative
  26. func _physics_process(delta):
  27. # reset the x and z velocity
  28. vel.x = 0
  29. vel.y = 0
  30. vel.z = 0
  31. var input = Vector3()
  32. # movement inputs
  33. if Input.is_action_pressed("move_forward"):
  34. input.y -= 1
  35. if Input.is_action_pressed("move_backward"):
  36. input.y += 1
  37. if Input.is_action_pressed("move_left"):
  38. input.x -= 1
  39. if Input.is_action_pressed("move_right"):
  40. input.x += 1
  41. if Input.is_action_pressed("move_up"):
  42. input.z += 1
  43. if Input.is_action_pressed("move_down"):
  44. input.z -= 1
  45. input = input.normalized()
  46. # get the forward and right directions
  47. var forward = global_transform.basis.z
  48. var right = global_transform.basis.x
  49. var up = global_transform.basis.y
  50. var relativeDir = (forward * input.y + right * input.x + up * input.z)
  51. var moveSpeed2 = moveSpeed
  52. if Input.is_action_pressed("sprint"):
  53. moveSpeed2 = moveSpeed * 3
  54. # set the velocity
  55. vel.x = relativeDir.x * moveSpeed2
  56. vel.z = relativeDir.z * moveSpeed2
  57. vel.y = relativeDir.y * moveSpeed2
  58. # move the player
  59. vel = move_and_slide(vel, Vector3.UP)