follow_camera.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. extends Camera
  2. # Member variables
  3. var collision_exception = []
  4. export var min_distance = 0.5
  5. export var max_distance = 3.5
  6. export var angle_v_adjust = 0.0
  7. export var autoturn_ray_aperture = 25
  8. export var autoturn_speed = 50
  9. var max_height = 2.0
  10. var min_height = 0
  11. func _physics_process(dt):
  12. var target = get_parent().get_global_transform().origin
  13. var pos = get_global_transform().origin
  14. var up = Vector3(0, 1, 0)
  15. var delta = pos - target
  16. # Regular delta follow
  17. # Check ranges
  18. if delta.length() < min_distance:
  19. delta = delta.normalized() * min_distance
  20. elif delta.length() > max_distance:
  21. delta = delta.normalized() * max_distance
  22. # Check upper and lower height
  23. if delta.y > max_height:
  24. delta.y = max_height
  25. if delta.y < min_height:
  26. delta.y = min_height
  27. # Check autoturn
  28. var ds = PhysicsServer.space_get_direct_state(get_world().get_space())
  29. var col_left = ds.intersect_ray(target, target + Basis(up, deg2rad(autoturn_ray_aperture)).xform(delta), collision_exception)
  30. var col = ds.intersect_ray(target, target + delta, collision_exception)
  31. var col_right = ds.intersect_ray(target, target + Basis(up, deg2rad(-autoturn_ray_aperture)).xform(delta), collision_exception)
  32. if !col.empty():
  33. # If main ray was occluded, get camera closer, this is the worst case scenario
  34. delta = col.position - target
  35. elif !col_left.empty() and col_right.empty():
  36. # If only left ray is occluded, turn the camera around to the right
  37. delta = Basis(up, deg2rad(-dt * autoturn_speed)).xform(delta)
  38. elif col_left.empty() and !col_right.empty():
  39. # If only right ray is occluded, turn the camera around to the left
  40. delta = Basis(up, deg2rad(dt *autoturn_speed)).xform(delta)
  41. else:
  42. # Do nothing otherwise, left and right are occluded but center is not, so do not autoturn
  43. pass
  44. # Apply lookat
  45. if delta == Vector3():
  46. delta = (pos - target).normalized() * 0.0001
  47. pos = target + delta
  48. look_at_from_position(pos, target, up)
  49. # Turn a little up or down
  50. var t = get_transform()
  51. t.basis = Basis(t.basis[0], deg2rad(angle_v_adjust)) * t.basis
  52. set_transform(t)
  53. func _ready():
  54. # Find collision exceptions for ray
  55. var node = self
  56. while node:
  57. if node is RigidBody:
  58. collision_exception.append(node.get_rid())
  59. break
  60. else:
  61. node = node.get_parent()
  62. set_physics_process(true)
  63. # This detaches the camera transform from the parent spatial node
  64. set_as_toplevel(true)