- Edited
I'm doing a car chase-camera which circles around the car depending on whether the car is going forward or backward, and I'm having the issue that it always circles around the same way, even when it's the longest way.
Just to explain my setup a bit: the camera is not child of the car, and I'm using two pivots on the camera, one that keeps interpolating itself to match the position and Y rotation of the car; and the other pivot rotates to turn the camera around the car. The camera simply does look_at(car)
. So my camera setup looks like this:
pivot1 (Spatial) # always following the car
pivot2 (Spatial) # always at local (0,0,0), rotates around Y
ClippedCamera # the cam itself is at some height and distance from the pivots
So the rotation of the second pivot is basically just from 0 to 180 and back. But it always goes the same way around.
Under the notion that sometimes the camera is to the left or to the right of the car, due to steering and the smoothing of pivot1 taking time to catch up to it, I tried comparing pivot1.rotation.y
with car.rotation.y
, to see if one was bigger than the other and vice-versa depending on which side the camera was from the car, but I couldn't get consistent results that I could use to control pivot2's rotation (I may have done something wrong, though).
I also tinkered naively with dot products of their positions and rotations, but that didn't yield any values that seemed useful, either.
My current code looks like this:
func _process(delta: float) -> void:
# pivot1 (self)
global_transform.origin = global_transform.origin.linear_interpolate(car.global_transform.origin, 0.075)
rotation.y = lerp_angle(rotation.y, car.rotation.y, 0.01)
# pivot2 (car_heading means going forward (1) or backward (-1) or idle (0))
if rotation_degrees.y-car.rotation_degrees.y < 0:
if car_heading > 0: target_rot = 0
elif car_heading < 0: target_rot = deg2rad(180)
else:
if car_heading > 0: target_rot = deg2rad(360) # this probably yields zero
elif car_heading < 0: target_rot = deg2rad(180)
pivot.rotation.y = lerp_angle(pivot.rotation.y, target_rot, 0.025)
# camera
camera.look_at(car.global_transform.origin, Vector3.UP)