Advertisement
RecluseStudio

Subnatica FP Controls Attempt

Dec 5th, 2020 (edited)
2,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody
  2.  
  3. #TODO
  4. #   forward and backward based on where player is looking
  5. #   space bar moves player up Y based on world coords at half mvmnt speed - DONE
  6. #   when pressing left and right, add smooth camera tilt in respective direction
  7.  
  8. export var speed = 10
  9. export var accel = 5
  10. export var mouse_sense = .09
  11.  
  12. onready var head = $Head
  13. onready var camera = $Head/Camera
  14. onready var point_camera = $Head/Camera/Point_Camera
  15.  
  16. var float_power = speed * .5
  17. var velocity = Vector3()
  18. var camera_x_rot = 0
  19.  
  20. func _ready():
  21.     #stops mouse pointer escaping game window
  22.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  23.  
  24. func _input(event):
  25.     # ESCAPE key
  26.     if Input.is_action_just_pressed("EXIT"):
  27.         get_tree().quit()
  28.        
  29.     #mouse controls where the player looks
  30.     if event is InputEventMouseMotion:
  31.         head.rotate_y(deg2rad(-event.relative.x * mouse_sense))
  32.         var x_delta = -event.relative.y * mouse_sense
  33.        
  34.         #locks the camera view to +- 90 degrees
  35.         if camera_x_rot + x_delta > -90 and camera_x_rot + x_delta < 90:
  36.             camera.rotate_x(deg2rad(x_delta))
  37.             camera_x_rot += x_delta
  38.    
  39.  
  40. func _physics_process(delta):
  41.     # get the local trasform of the head spatial node
  42.     var head_basis = head.get_global_transform().basis
  43.     var direction = Vector3()
  44.     # get the direction the player is looking at
  45.     var look_direction = point_camera.get_global_transform().origin - camera.get_global_transform().origin
  46.    
  47.     if Input.is_action_pressed("move_forwards"):
  48. #       direction -= head_basis.z
  49.         direction += look_direction
  50.     elif Input.is_action_pressed("move_backwards"):
  51.         direction -= look_direction
  52.     # new IF statement so we can move f/b AND r/l at same time
  53.     if Input.is_action_pressed("move_left"):
  54.         direction -= head_basis.x
  55.     elif Input.is_action_pressed("move_right"):
  56.         direction += head_basis.x
  57.  
  58.     if Input.is_action_pressed("float"):
  59.         direction += head_basis.y  
  60.     if Input.is_action_pressed("sink"):
  61.         direction -= head_basis.y
  62.        
  63.     # if moving f/b AND  r/l, it will double the speed unless normalised
  64.     look_direction = look_direction.normalized()
  65.     direction = direction.normalized()
  66.        
  67.     velocity = velocity.linear_interpolate(direction * speed, accel * delta)
  68.  
  69.     # move and slide allows collisions for kinematic bodies(i think)
  70.     velocity = move_and_slide(velocity)
  71.        
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement