23 lines
1.3 KiB
GDScript
23 lines
1.3 KiB
GDScript
extends Node3D
|
|
|
|
@export var AnimatedSprite: AnimatedSprite2D # 2d sprite
|
|
@export var Camera: Camera3D # current camera
|
|
@export var CharacterBody: Node3D # 3d object
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
# TODO: optimize to ensure it doesn't use CPU when both camera and object is idle
|
|
func _process(_delta):
|
|
const Z_MULTIPLIER = 10 # number of z indexes that 1m will take
|
|
# set 2d sprite z index (render order) to 3d object z coordinate, multiply by Z_MULTIPLIER
|
|
AnimatedSprite.z_index = CharacterBody.global_position.z * Z_MULTIPLIER
|
|
#print_debug(AnimatedSprite.z_index)
|
|
|
|
if not Camera.is_position_behind(CharacterBody.global_position): # if not outide of camera range
|
|
AnimatedSprite.global_position = Camera.unproject_position(CharacterBody.global_position) # set sprite position to where 3d object is from camera's POV
|
|
var distance = pow(CharacterBody.global_position.distance_to(Camera.global_position),0.25) # calculate distance between camera and 3d object and smooth it out
|
|
if distance < 2:
|
|
AnimatedSprite.scale = Vector2(2,2) - Vector2(distance,distance) # the more distance the smaller is the scale of sprite
|
|
#print_debug(AnimatedSprite.scale)
|
|
else:
|
|
AnimatedSprite.scale = Vector2(0,0) # is distance big, prevent from going to negative
|