Camera.gd 1011 B

123456789101112131415161718192021222324252627282930313233343536
  1. extends Camera
  2. export var target_node: NodePath
  3. export var vehicle_controller_node: NodePath
  4. export var fixed_angle: bool = true
  5. var target: Spatial
  6. var vehicle_controller: Spatial
  7. var distance_measured: Vector3
  8. var distance_magnitude: float
  9. func _ready():
  10. target = get_node(target_node)
  11. vehicle_controller = get_node(vehicle_controller_node)
  12. vehicle_controller.visible = fixed_angle
  13. distance_measured = target.global_transform.origin - global_transform.origin
  14. distance_magnitude = distance_measured.length()
  15. func _physics_process(_delta: float):
  16. if fixed_angle:
  17. follow()
  18. else:
  19. view_from_inside()
  20. func view_from_inside():
  21. var rotation = Quat(target.global_transform.basis)
  22. var euler: Vector3 = rotation.get_euler()
  23. euler.z = 0
  24. rotation = Quat(euler)
  25. transform.basis = Basis(rotation)
  26. transform.origin = target.global_transform.origin
  27. func follow():
  28. var rotation = Quat(transform.basis)
  29. transform.origin = target.global_transform.origin - rotation * Vector3.FORWARD * distance_magnitude