Help with 3d fighting game camera

Godot Version

Godot4.5

Question

(sorry for bad english in advance)

I’m new to godot, so i did what every newbie does and searched guides to help me . one of the things that i found trouble in, is making a fighting game 3d camera that functions like in street fighter 4~6. and the only video that i found that talks about this subject matter is this one. while it functions it has the minor bug of the z position going back and forth, making the camera zoom in and out

my only good guess as to why this happens is that adding movement_speed variable to the z position makes it so the current proportion smaller than the initial one in the following frame, triggering the elif statement and the subtraction happens returning the current proportion value to it’s initial state and triggering the if statement in the following frame. here is the code (ignore the prints those are for debugging) :

func calculate_position(rect: Rect2, unprojected_rect: Rect2) -> Vector3:
	var center : = calculate_center(rect)
	
	if initial_proportion == 0 or initial_distance == 0:
		initial_proportion = unprojected_rect.size.x / viewport_rect.size.x
		initial_distance = position.distance_to(get_child(0).position)
	
	var z_position :  = position.z
	if unprojected_rect.size.x / viewport_rect.size.x > initial_proportion:
		z_position = position.z + move_speed
		print("t1")
		print(unprojected_rect.size.x / viewport_rect.size.x > initial_proportion)
		print(unprojected_rect.size.x / viewport_rect.size.x )
		print( initial_proportion)
	elif unprojected_rect.size.x / viewport_rect.size.x < initial_proportion and z_position > initial_distance:
		z_position = position.z -move_speed
		print("t2")
		print(unprojected_rect.size.x / viewport_rect.size.x < initial_proportion and z_position > initial_distance)
		print(unprojected_rect.size.x / viewport_rect.size.x )
		print( initial_proportion)
	
	return Vector3(center.x + center_offset.x, center.y + center_offset.y, lerp(position.z, z_position, smoothing))

here is debug sample that shows what happens:

Well it definitely zooms in and out because of the z_position is changed in if and elif, can see no other reason. What you are trying to say I think, is that it moves in and out all the time without ever settling anywhere?

Notice that there is a third implied case in your conditions:

if
 // zoom in
elif
 // zoom out
else
 // do neither

meaning it would stop zooming, but the way you set up the initial values must never allows it to get there.

You probably shouldn’t initialize initial_distance from position.distance_to() and then compare it to position.z . Those two are unrelated and I guess that could be a part of the problem. But more importantly I think you should settle on either comparing rect sizes or distances, and then just program plain and simple how much for that single value is too far, how much is to close, and leave some space between the two so the camera could settle there. That would make the code much easier to understand and debug.

1 Like

Thanks for the help!