Player.gd 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #Johann Massyn, 23/06/2019
  2. #Godot (3.1) 3D Player movement script
  3. #Atatch script to 3d node with following structure
  4. #Kinematic body: Player
  5. # - CollisionShape: CollisionShape
  6. # - Camera: Camera
  7. extends KinematicBody
  8. export (bool) var can_move = true #Alow player to input movment.
  9. export (bool) var can_sprint = true #Alow player to toggle sprint movment.
  10. export (float) var move_speed = 8 #Players movement speed
  11. export (float) var move_speed_sprint = 16#16 #Players sprint movement speed
  12. export (bool) var move_sprint = false #Player sprinting toggle
  13. export (float) var move_speed_max = 200 #Player maximum velocity
  14. export (float) var move_acceleration = 7 #Players acceleration to movment speed
  15. export (float) var move_deacceleration = 10 #Players deacceleration from movment speed
  16. export (bool) var mouse_captured = true #Toggles mouse captured mode
  17. export (float) var mouse_sensitivity_x = 0.3 #Mouse sensitivity X axis
  18. export (float) var mouse_sensitivity_y = 0.3 #Mouse sensitivity Y axis
  19. export (float) var mouse_max_up = 45 #Mouse max look angle up
  20. export (float) var mouse_max_down = -50 #Mouse max look angle down
  21. export (float) var jump_speed = 6 #Players jumps speed
  22. export (float) var jump_speed_sprint = 12 #Players jumps speed
  23. export (bool) var allow_fall_input = true #Alow player to input movment when falling
  24. export (bool) var stop_on_slope = false #Toggle sliding on slopes
  25. export (float) var max_slides = 4 #Maximum of slides
  26. export (float) var floor_max_angle = 60 #Maximum slop angle player can traverse
  27. export (bool) var infinite_inertia = false #Toggle infinite inertia
  28. export (float) var gravaty = 1.625 #9.81 #Gravaty acceleration
  29. #export (Vector3) var gravaty_vector = Vector3(0, -1, 0) #Gravaty normal direction vector
  30. #export (Vector3) var floor_normal = Vector3(0, 1, 0) #Floor normal direction vector
  31. #export (Vector3) var jump_vector = Vector3(0, 1, 0) #Jump normal direction vector
  32. export (Vector3) var velocity = Vector3(0, 0, 0) #Initial velocity
  33. onready var up = global_transform.origin.normalized()
  34. onready var forward = global_transform.basis.y
  35. onready var right = forward.cross(up).normalized()
  36. var maxheight = 10.783
  37. var minheight = -8.719
  38. var radius = 1737.4
  39. export (int) var move_mode = 0
  40. enum moveMode {MODE_NORMAL, MODE_HOVER, MODE_FLY}
  41. var height_over_ground = 0
  42. func _ready():
  43. if mouse_captured:
  44. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  45. pass
  46. func moveTo(pos = Vector3()):
  47. if pos.z == 0:
  48. pos.z = radius
  49. move_and_slide(Common.latLonToGlobal(Vector3(pos.x,pos.y,pos.z)), Vector3.UP)
  50. func getSpos():
  51. var sPos = Common.globalToLatLon(self.global_transform.origin)
  52. sPos.y = Common.relativeToAbsolute(sPos.y)
  53. return sPos
  54. func _process(delta):
  55. var pos = self.global_transform.origin
  56. .get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
  57. .get_parent().get_node("UI/2").text="latlonhight: "+str(getSpos())+","+str(pos.length())
  58. .get_parent().get_node("UI/3").text="vel: %s" % velocity
  59. .get_parent().get_node("UI/4").text="mode: %s" % move_mode
  60. height_over_ground = pos.length() - radius*1000
  61. #floor_normal = pos.normalized()
  62. #gravaty_vector = floor_normal * -1
  63. #self.transform.basis.y = floor_normal
  64. #rotation_degrees.x = 90
  65. func _physics_process(delta):
  66. up = global_transform.origin.normalized()
  67. right = forward.cross(up).normalized()
  68. forward = up.cross(right).normalized()
  69. transform.basis = Basis(right, up, -forward)
  70. # we differentialte the y portion to apply a different speed (jump_speed)
  71. var horDir = Vector3()
  72. var vertDir = Vector3()
  73. match move_mode:
  74. moveMode.MODE_FLY:
  75. horDir = forward
  76. moveMode.MODE_HOVER:
  77. horDir = -velocity * 0.1
  78. if can_move and (is_on_floor() or allow_fall_input):
  79. #Left
  80. if Input.is_action_pressed("move_left"):
  81. horDir -= right
  82. #Right
  83. if Input.is_action_pressed("move_right"):
  84. horDir += right
  85. #Forward
  86. if Input.is_action_pressed("move_forward"):
  87. horDir += forward
  88. #Backwards
  89. if Input.is_action_pressed("move_backward"):
  90. horDir -= forward
  91. # Jump
  92. #if is_on_floor():
  93. #if Input.is_action_pressed("move_up"):
  94. # dir += up
  95. if Input.is_action_pressed("move_down"):
  96. vertDir -= up
  97. #Jump
  98. if Input.is_action_pressed("move_up"):# and is_on_floor():
  99. vertDir += up
  100. #Sprint toggle
  101. if can_sprint and Input.is_action_just_pressed("move_sprint"): #and is_on_floor():
  102. move_sprint = true
  103. if can_sprint and not Input.is_action_pressed("move_sprint"): #and is_on_floor():
  104. move_sprint = false
  105. if move_sprint:
  106. move_speed_max = 600 + 2000 * inverse_lerp(minheight*1000, maxheight*1000, height_over_ground)
  107. else:
  108. move_speed_max = 200 + 800 * inverse_lerp(minheight*1000, maxheight*1000, height_over_ground)# height_over_ground * 100 + 400
  109. #Hover toggle
  110. if Input.is_action_just_pressed("hover_mode"):
  111. if move_mode != moveMode.MODE_HOVER:
  112. move_mode = moveMode.MODE_HOVER
  113. else:
  114. move_mode = moveMode.MODE_NORMAL
  115. #Fly toggle
  116. if Input.is_action_just_pressed("fly_mode"):
  117. if move_mode != moveMode.MODE_FLY:
  118. move_mode = moveMode.MODE_FLY
  119. else:
  120. move_mode = moveMode.MODE_NORMAL
  121. if move_mode == moveMode.MODE_NORMAL and horDir+vertDir == Vector3(0, 0, 0):
  122. # in NORMAL MODE velocity stays unchanged without input
  123. pass
  124. else:
  125. #velocity = velocity.linear_interpolate(horDir * (move_speed_sprint if move_sprint else move_speed)
  126. # + vertDir * (jump_speed_sprint if move_sprint else jump_speed), move_acceleration * delta)
  127. velocity += (horDir * (move_speed_sprint if move_sprint else move_speed)
  128. + vertDir * (jump_speed_sprint if move_sprint else jump_speed)) * move_acceleration * delta
  129. #velocity = velocity.linear_interpolate(vertDir * (jump_speed_sprint if move_sprint else jump_speed), move_acceleration * delta)
  130. #if !is_on_floor():
  131. #velocity -= up * gravaty
  132. # limit Speed
  133. if velocity.length() > move_speed_max:
  134. velocity = velocity.normalized() * move_speed_max
  135. move_and_slide(velocity, up)
  136. #var collide = move_and_collide(velocity, true, true, true)
  137. #if collide:
  138. # velocity = Vector3()
  139. #else:
  140. # transform.origin += velocity
  141. #Smooth movement
  142. #dir = transform.basis.xform(dir.normalized()) * (move_speed_sprint if move_sprint else move_speed)
  143. #if is_on_floor() or dir != Vector3(0, 0, 0):
  144. # var acceleration = move_acceleration if dir.dot(velocity) else move_deacceleration
  145. # var vp = gravaty_vector.normalized() * velocity.dot(gravaty_vector)
  146. # velocity = (velocity - vp).linear_interpolate(dir, acceleration * delta) + vp
  147. #Gravaty
  148. #if !is_on_floor():
  149. # velocity += gravaty_vector * gravaty * delta
  150. #Player move
  151. #move_and_slide(velocity, floor_normal, stop_on_slope, max_slides, deg2rad(floor_max_angle), infinite_inertia)
  152. #Player orientation
  153. #var spos = Common.globalToLatLon(self.global_transform.origin)
  154. #global_rotate(Vector3(1,0,0), deg2rad(spos.x))
  155. #global_rotate(Vector3(0,1,0), deg2rad(spos.y))
  156. func _input(event):
  157. #Mouse movement
  158. if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  159. #rotate_y(deg2rad(-event.relative.x * mouse_sensitivity_x))
  160. forward = forward.rotated(up, deg2rad(-event.relative.x * mouse_sensitivity_x))
  161. #rotate_y(deg2rad(-event.relative.x * mouse_sensitivity_x))
  162. #self.rotation_degrees.y += -event.relative.x * mouse_sensitivity_x
  163. $Camera.rotation_degrees.x = clamp($Camera.rotation_degrees.x + -event.relative.y * mouse_sensitivity_y, mouse_max_down, mouse_max_up)
  164. pass