Advertisement
Creepinson

Godot FPS Player Controller Script

Jun 17th, 2020
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. export var speed = 10
  4. export var acceleration = 5
  5. export var gravity = 0.98
  6. export var jump_power = 30
  7. export var mouse_sensitivity = 0.3
  8.  
  9. onready var head = $Head
  10. onready var camera = $Head/Camera
  11.  
  12. var mouseToggled = false
  13. var velocity = Vector3()
  14. var camera_x_rotation = 0
  15.  
  16. func _ready():
  17. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  18. mouseToggled = true
  19.  
  20. func _input(event):
  21. if event is InputEventMouseMotion:
  22. head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
  23.  
  24. var x_delta = event.relative.y * mouse_sensitivity
  25. if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90:
  26. camera.rotate_x(deg2rad(-x_delta))
  27. camera_x_rotation += x_delta
  28.  
  29. func _process(delta):
  30. if Input.is_action_just_pressed("ui_cancel"):
  31. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE if (mouseToggled) else Input.MOUSE_MODE_CAPTURED)
  32. mouseToggled = !mouseToggled
  33.  
  34. func _physics_process(delta):
  35. var head_basis = head.get_global_transform().basis
  36.  
  37. var direction = Vector3()
  38. if Input.is_action_pressed("move_forward"):
  39. direction -= head_basis.z
  40. elif Input.is_action_pressed("move_back"):
  41. direction += head_basis.z
  42.  
  43. if Input.is_action_pressed("move_left"):
  44. direction -= head_basis.x
  45. elif Input.is_action_pressed("move_right"):
  46. direction += head_basis.x
  47.  
  48. direction = direction.normalized()
  49.  
  50. velocity = velocity.linear_interpolate(direction * speed, acceleration * delta)
  51. velocity.y -= gravity
  52.  
  53. if Input.is_action_just_pressed("jump") and is_on_floor():
  54. velocity.y += jump_power
  55.  
  56. velocity = move_and_slide(velocity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement