0 votes

How do i switch to the nearest camera and switch back to a player camera?

I have a player with a firstperson camera, which i want to be able to switch to another camera, which should be the nearest camera from the player.

Godot version 3.4.4-stable
in Engine by (64 points)

1 Answer

0 votes
Best answer

First of all, I am not sure if you are asking how to switch cameras in general. Assuming you know how to do that, and you are asking how to know which camera is the closest one, one way would be to add all the cameras to a group, lets say "cameras", and then iterate looking for the closest one like this: (pseudo-code, since I dont recall exactly how certain methods are written):

var closest_distance = 0
var closest_camera
for camera in get_nodes_in_group("cameras"):
    var distance = self.position.distance_to(camera.position)
    if closest_distance == 0: 
        closest_distance = distance
        closest_camera = camera
    if distance < closest_distance:
        closest_distance = distance
        closest_camera = camera
by (418 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.