Advertisement
froggytxt

fps controller

Mar 31st, 2021
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody
  2.  
  3. var speed = 10
  4. var mouse_sen = 0.10
  5. var h_acc = 6
  6. var gravity = 20
  7. var jump = 10
  8. var full_contact = false
  9. var air_acc = 1
  10. var normal_acc = 6
  11.  
  12.  
  13. var dir = Vector3()
  14. var h_vel = Vector3()
  15. var movement = Vector3()
  16. var gravity_vec = Vector3()
  17.  
  18. onready var head = $head
  19. onready var groundcheck = $groundcheck
  20. func _ready():
  21.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  22.  
  23.  
  24. func _input(event):
  25.     if event is InputEventMouseMotion:
  26.         rotate_y(deg2rad(-event.relative.x * mouse_sen))
  27.         head.rotate_x(deg2rad(-event.relative.y * mouse_sen))
  28.         head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
  29. func _physics_process(delta):
  30.     dir = Vector3()
  31.    
  32.    
  33.     if groundcheck.is_colliding():
  34.         full_contact = true
  35.     else:
  36.         full_contact = false
  37.    
  38.    
  39.     if not is_on_floor():
  40.         gravity_vec += Vector3.DOWN * gravity * delta
  41.         h_acc = air_acc
  42.     elif is_on_floor() and full_contact:
  43.         gravity_vec = -get_floor_normal() * gravity
  44.         h_acc = normal_acc
  45.     else:
  46.         gravity_vec = -get_floor_normal()
  47.         h_acc = normal_acc
  48.    
  49.    
  50.    
  51.    
  52.     if Input.is_action_just_pressed("jump") and (is_on_floor() or groundcheck.is_colliding()):
  53.         gravity_vec = Vector3.UP * jump
  54.    
  55.     if Input.is_action_pressed("move_forward"):
  56.         dir -= transform.basis.z
  57.     elif Input.is_action_pressed("move_backwards"):
  58.         dir += transform.basis.z
  59.     if Input.is_action_pressed("move_left"):
  60.         dir -= transform.basis.x
  61.     elif Input.is_action_pressed("move_right"):
  62.         dir += transform.basis.x
  63.        
  64.     dir = dir.normalized()
  65.     h_vel = h_vel.linear_interpolate(dir * speed, h_acc * delta)
  66.     movement.z = h_vel.z + gravity_vec.z
  67.     movement.x = h_vel.x + gravity_vec.x
  68.     movement.y = gravity_vec.y
  69.     move_and_slide(movement, Vector3.UP)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement