Advertisement
Guest User

Gimbal Camera Code

a guest
Feb 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. extends Spatial
  2.  
  3. export var ZoomSpeed = 0.25
  4. export var RotateSpeed = 0.15
  5. export var MaxZoomDistance = 15.0
  6. export var MedZoomDistance = 6.0
  7. export var MinZoomDistance = 3.0
  8. export var MinClampXAngle = 70
  9. export var MaxClampXAngle = -35
  10. export var CameraYAdjust = 1.75
  11.  
  12. var distance
  13. var target
  14. var player
  15.  
  16. var zoom_level = 1
  17.  
  18. # probe variables
  19. var left_probe
  20. var right_probe
  21. var back_probe
  22. var head_probe
  23. var butt_probe # I am SOOO mature...
  24. var probes = [] # this array holds the probes, so we can detect collision in a for loop
  25.  
  26. var cam
  27.  
  28. var occluded
  29.  
  30.  
  31. # Called when the node enters the scene tree for the first time.
  32. func _ready():
  33. # set as the top level
  34. set_as_toplevel(true)
  35.  
  36. # store the player node (ALWAYS make sure the player is the DIRECT parent of YROTATE!!!)
  37. player = get_parent()
  38.  
  39. # store the target node
  40. target = get_node("XRotate/Target")
  41.  
  42. # hide the helper mesh
  43. $XRotate/HelperMesh.hide()
  44.  
  45. # set the camera to the min distance at start
  46. target.transform.origin.z = MinZoomDistance
  47.  
  48. # store the camera
  49. cam = $XRotate/InterpolatedCamera
  50.  
  51. # store the probe nodes
  52. left_probe = $XRotate/InterpolatedCamera/Probes/Left
  53. right_probe = $XRotate/InterpolatedCamera/Probes/Right
  54. back_probe = $XRotate/InterpolatedCamera/Probes/Back
  55. head_probe = $XRotate/InterpolatedCamera/Probes/Head
  56. butt_probe = $XRotate/InterpolatedCamera/Probes/Butt
  57.  
  58. # store the probes in an array
  59. probes = [left_probe, right_probe, back_probe, head_probe, butt_probe]
  60.  
  61. # orient target to player
  62. target.look_at(player.transform.origin, Vector3(0, 1, 0))
  63.  
  64. func _physics_process(delta):
  65. # set the current distance
  66. distance = target.transform.origin.z
  67.  
  68. # get rotation input from controller
  69. var xRotateAmount = Input.get_action_strength("cam_down") - Input.get_action_strength("cam_up")
  70. var yRotateAmount = Input.get_action_strength("cam_right") - Input.get_action_strength("cam_left")
  71.  
  72. # rotate xRotate node
  73. var cam_rot = $XRotate.rotation_degrees
  74. cam_rot.x = clamp(cam_rot.x, MaxClampXAngle, MinClampXAngle)
  75. $XRotate.rotation_degrees = cam_rot
  76. $XRotate.rotate_x(-xRotateAmount * RotateSpeed)
  77.  
  78. # rotate y
  79. rotate_y(-yRotateAmount * RotateSpeed)
  80.  
  81. # set the distance of the camera
  82. _update_camera()
  83.  
  84.  
  85. func _update_camera():
  86.  
  87. var toggle_zoom = Input.is_action_just_pressed("toggle_distance")
  88.  
  89. # if we toggle the zoom, increase the zoom level
  90. if toggle_zoom:
  91. zoom_level += 1
  92. if zoom_level > 3:
  93. zoom_level = 1
  94.  
  95. # set the distance, depending on the zoom level
  96. if zoom_level == 1:
  97. target.transform.origin.z = MinZoomDistance
  98. elif zoom_level == 2:
  99. target.transform.origin.z = MedZoomDistance
  100. elif zoom_level == 3:
  101. target.transform.origin.z = MaxZoomDistance
  102.  
  103. # check if the player is being occluded or not
  104. _detect_occlusion()
  105.  
  106. # check for camera collisions
  107. if !occluded:
  108. _camera_collisions()
  109.  
  110.  
  111. func _detect_occlusion():
  112. var look_for_player = get_world().get_direct_space_state()
  113. var obstacle = look_for_player.intersect_ray(target.global_transform.origin, player.transform.origin)
  114. if !obstacle.empty():
  115. # store the previous position of the target node, so that we can return to it
  116. var prev_position = target.global_transform.origin
  117.  
  118. # if the obstacle is not the player, move the target such that it isn't inside of the obstacle
  119. if !obstacle.collider.is_in_group("Player"):
  120. occluded = true
  121. print("hit object " + str(obstacle.collider) + " at " + str(obstacle.position))
  122. # move the target to the player
  123. target.global_transform.origin = player.transform.origin
  124. else:
  125. # restore the target's previous position
  126. target.global_transform.origin = prev_position
  127. occluded = false
  128.  
  129. func _camera_collisions():
  130. var prev_global_position = target.global_transform.origin
  131. var prev_local_position = target.transform.origin
  132.  
  133. # loop through the probes array and check if any of them collider with the environment
  134. for probe in probes:
  135. if probe.is_colliding():
  136. print(str(probe.get_collider()))
  137. # zoom in on the player
  138. target.transform.origin.z = MinZoomDistance-1
  139. # look at the player
  140. target.look_at(player.transform.origin, Vector3(0, 1, 0))
  141. else:
  142. target.transform.origin = prev_local_position
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement