Billboard 8-way sprite rotating relative to camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Moot Point

Hello. I have a Sprite3D set up with 8-way animations, using an animation tree to handle which way it should look at. It works right now when I have a static camera, it’ll show the correct animation. I’m using the blend position parameter with a vector that holds my inputs, but when I’m idling in a scene with the camera rotating I’d like for it to reflect on my character as well.

	tree.set("parameters/Walk/blend_position", input_vector)
	tree.set("parameters/Attack/blend_position", input_vector)
	tree.set("parameters/Idle/blend_position", input_vector)

This is how it’s set up on the code right now, what would need to change on the idle one for it to update according to the camera rotation? Any code example also appreciated.

Edit: Forgot to add the camera for my characters is handled by a spring arm node.

:bust_in_silhouette: Reply From: aXu_AP

Here’s my answer to somewhat similiar problem. [GDScript] 2.5D directional sprites - Godot Forums

I’m no expert in 3D, but here’s my 2 cents. First of all, I think the orientation of the camera shouldn’t matter but the relative position of camera - even if a sprite comes towards you from your left side, it should show a forward sprite, not right sprite. Second thing I’d do is making this problem 2D by leaving y axis out of the equation.

Something like this (untested, sorry if it doesn’t work as expected!):

var pos_2d = Vector2(global_transform.origin.x, global_transform.origin.z)
var camera_pos_2d = Vector2(camera.global_transform.origin.x, camera.global_transform.origin.z)
var camera_pos_2d_local = pos_2d - camera_pos_2d
var forward = global_transform.basis.z
var forward_2d = Vector2(forward.x, forward.z)
var angle = forward_2d.angle_to(camera_pos_2d_local)

Use this angle to determine actual sprite. Close to 0 should be forward, around PI backwards.

Unfortunately I didn’t get confirmation did it work out in the end.