Here are my code with comments:
tool
extends Spatial
var min_distance = 5
var max_distance = 10
func _process(delta):
var target = get_node("../ObjectB")
# get global transforms
var target_t: Transform = target.get_global_transform_interpolated()
var own_t: Transform = get_global_transform_interpolated()
# get offset (direction and length) from target (origin) to follower position
var offset = own_t.origin - target_t.origin
# if offset length is not within minimum and maximum distances
if offset.length() > max_distance:
offset = offset.normalized() * max_distance
if offset.length() < min_distance:
offset = offset.normalized() * min_distance
# normalize offset to know only direction
var normalized_offset = offset.normalized()
# get dot product of same two vectors to know the angle between them
# (z axis points to the same direction as object's face)
var dot = target_t.basis.z.dot(normalized_offset)
# get cross product of z axis and offset and normalize it
# to know in which direction rotate offset vector
# (order of operation matters)
var normal = normalized_offset.cross(target_t.basis.z).normalized()
# cross product will return (0,0,0) if two vectors are aligned
# and it won't be possible to use the reslut of the cross product as axis
if normal != Vector3.ZERO:
# get rad angle from the result of the dot product
# and rotate offset along normal axis
# (if rotate not with additional PI, offset and z axis will be aligned
# and follower will be in front of target, because offset is a vector
# from target position to follower position)
offset = offset.rotated(normal, PI + acos(dot))
# find new follower position by adding vector facing follower to target position
var new_position: Vector3 = target_t.origin + offset
# gradually move to new position
new_position = lerp(own_t.origin, new_position, 0.05)
# change follower position and rotation
look_at_from_position(new_position, target_t.origin, Vector3.UP)
I am new to Godot. Is there a built-in tool or some option for that? I wrote it for a camera, changing the Godot's vehicle example.