follow_camera.gd 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. extends Camera
  2. # Member variables
  3. var collision_exception = []
  4. export var min_distance = 0.5
  5. export var max_distance = 4.0
  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 _fixed_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. pos = target + delta
  28. look_at_from_pos(pos, target, up)
  29. # Turn a little up or down
  30. var t = get_transform()
  31. t.basis = Matrix3(t.basis[0], deg2rad(angle_v_adjust))*t.basis
  32. set_transform(t)
  33. func _ready():
  34. # Find collision exceptions for ray
  35. var node = self
  36. while(node):
  37. if (node extends RigidBody):
  38. collision_exception.append(node.get_rid())
  39. break
  40. else:
  41. node = node.get_parent()
  42. set_fixed_process(true)
  43. # This detaches the camera transform from the parent spatial node
  44. set_as_toplevel(true)