Advertisement
Guest User

Player Controller

a guest
Aug 10th, 2022
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Spatial
  2.  
  3. onready var player = get_parent()
  4.  
  5. var rotation_helper
  6. var terminal = null
  7.  
  8. #movement variables
  9. var velocity := Vector3.ZERO
  10. export (float) var speed = 7.0
  11. export (float) var gravity = -0.98
  12. onready var MOUSE_SENSITIVITY = PlayerSettings.settings["Gameplay"]["MouseSensitivity"]
  13.  
  14. func _ready():
  15.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  16.  
  17.  
  18. func _unhandled_input(event):
  19.     if event.is_action_pressed("activate"):
  20.         if terminal and player.has_method("activate_terminal"):
  21.             player.activate_terminal(terminal)
  22.  
  23.     if event.is_action_pressed("reload"):
  24.         if player.has_method("do_reload"):
  25.             player.do_reload()
  26.  
  27.     if event is InputEventMouseButton:
  28.         if event.button_index ==  BUTTON_LEFT and event.pressed:
  29.             if player.has_method("do_left_click"):
  30.                 player.do_left_click()
  31.                
  32.         if event.button_index ==  BUTTON_RIGHT and event.pressed:
  33.             if player.has_method("do_right_click"):
  34.                 player.do_right_click()
  35.  
  36.     if event.is_action_pressed("mouse_release"):
  37.         Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  38.     elif event.is_action_released("mouse_release"):
  39.         Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  40.     if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  41.         rotation_helper = -event.relative.x / 80 * MOUSE_SENSITIVITY
  42.         player.global_rotate(Vector3.UP, rotation_helper)
  43.  
  44.  
  45.  
  46. func _physics_process(_delta):
  47.     var move_direction := Vector3.ZERO
  48.     move_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  49.     move_direction.z = Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
  50.     move_direction = move_direction.normalized()
  51.     move_direction = move_direction.rotated(Vector3.UP, player.global_rotation.y)
  52.  
  53.     velocity.x = move_direction.x * speed
  54.     velocity.z = move_direction.z * speed
  55.     if not player.is_on_floor():
  56.         velocity.y = gravity * speed
  57.  
  58.     velocity = player.move_and_slide(velocity, Vector3.UP, true)
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement