3dplayer.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. extends KinematicBody
  2. var gravity = Vector3.DOWN * 12 # strength of gravity
  3. var speed = 4 # movement speed
  4. var jump_speed = 6 # jump strength
  5. var spin = 0.1 # rotation speed
  6. var velocity = Vector3()
  7. var jump = false
  8. var old_pos = Vector3()
  9. var since_last_post = 0
  10. var post_interval = 250
  11. func get_input():
  12. var vy = velocity.y
  13. velocity = Vector3()
  14. if Input.is_action_pressed("move_forward"):
  15. velocity += -transform.basis.z * speed
  16. if Input.is_action_pressed("move_back"):
  17. velocity += transform.basis.z * speed
  18. if Input.is_action_pressed("strafe_right"):
  19. velocity += transform.basis.x * speed
  20. if Input.is_action_pressed("strafe_left"):
  21. velocity += -transform.basis.x * speed
  22. velocity.y = vy
  23. if Input.is_action_pressed("rotate_ccw"):
  24. $CameraHub.rotate_y(0.01)
  25. if Input.is_action_pressed("rotate_cw"):
  26. $CameraHub.rotate_y(-0.01)
  27. jump = false
  28. if Input.is_action_just_pressed("jump"):
  29. jump = true
  30. func _physics_process(delta):
  31. velocity += gravity * delta
  32. get_input()
  33. if jump and is_on_floor():
  34. velocity.y = jump_speed
  35. velocity = move_and_slide(velocity, Vector3.UP)
  36. func _unhandled_input(event):
  37. if event is InputEventMouseMotion:
  38. if event.relative.x > 0:
  39. rotate_y(-lerp(0, spin, event.relative.x/10))
  40. elif event.relative.x < 0:
  41. rotate_y(-lerp(0, spin, event.relative.x/10))
  42. func _process(delta):
  43. since_last_post += delta
  44. if since_last_post >= post_interval/1000 and get_tree().has_network_peer():
  45. if old_pos != self.get_global_transform().origin or since_last_post >= post_interval/100:
  46. rpc_id(0, "setTarget", get_tree().get_network_unique_id(), self.get_global_transform())
  47. old_pos = self.get_global_transform().origin
  48. since_last_post = 0
  49. remote func setTarget(id, trans):
  50. get_node("../RemotePlayers/Player "+str(id)).set_target(trans)