Basicly it is a floating TPV camera that looks at & moving towards the player, it works fine, and you can rotate the camera around the player, and up & down, works fine too. But the problem is: if you rotates the camera around and up/down together the camera moves backwards and jumps.
##The camera movement, works fine
func _process(delta: float) -> void:
var t = $tpvcam.global_transform.origin
var c = character.global_transform.origin
var y = cam.pos.y*cam.range_pos #camera height & range
var p = Vector3(c.x,c.y+y,c.z)
var ndir = p.direction_to(t)
var npos = p+(ndir*(cam.range*0.25))
var cpy = (c.y+y) - t.y
$tpvcam.global_transform.origin += (npos-t)*(delta*5)
if abs(cpy) > 0: #camera height adjustment
$tpvcam.global_transform.origin.y += cpy*delta*2
$tpvcam.look_at(c)
THe camera rotating around player, works fine if you rotate horizontal or vertical, but with both the camera moves backwards and jumps around.
func _input(event):
if event is InputEventMouseMotion
var relx = event.relative.x
var rely = event.relative.y
var t = $tpvcam.global_transform.origin
var c = character.global_transform.origin
var d = c.distance_to(t)
## i guess the main problem occurs from here
var rot = Vector2(t.x-c.x,t.z-c.z).angle() + relx*0.001
$tpvcam.global_transform.origin = c + Vector3(cos(rot) * d, t.y-c.y, sin(rot) * d)
$tpvcam.look_at(c)
##camera height
if rely < 0 and cam.pos.y > cam.range_pos*0.01:
cam.pos.y -= 0.01
elif rely > 0 and cam.pos.y < cam.range_pos*0.04:
cam.pos.y += 0.01
when you shows it, the problem are when the height are calculated with the distance, because the height increases the distance to the player.
So i replaced
var d = c.distance_to(t)
with
var d = Vector2(c.x,c.z).distance_to(Vector2(t.x,t.z))