Hi, I'm trying to achieve the effect of having one character turn to look at another in 3D. Rather than just pointing them like a model, though, my characters are billboarded AnimatedSprite3Ds, so they'll simply flip to reflect whether the player character is on their left or right. Think Paper Mario, I suppose, but with a free-roaming camera. The characters always face the camera and only have side-facing sprites, so they flip horizontally to look left or right.
I'm new to Godot and 3D in general, so I might be missing something obvious about 3D transforms or dot matrices. I achieved this effect in a Unity project with C# fairly simply, but Godot handles transforms differently from what I've read. This is the functional code from the old Unity project:
Vector3 dir = (target.transform.position - gameObject.transform.position).normalized;
float direction = Vector3.Dot(dir, gameObject.transform.right);
if (direction < 0)
{
gameObject.transform.localScale = new Vector3(-1, 1, 1);
}
if (direction > 0)
{
gameObject.transform.localScale = new Vector3(1, 1, 1);
}
I can't call "transform.right" the same way in GDScript, as far as I know. I'm no expert at this, so I've kind of been throwing things at the wall and seeing what sticks... but I'm close. I have this code at the moment, but it's working on the Z-axis. The character's sprite flips only when passing them from front to back instead of left to right. I'm not entirely sure why.
var dir := Vector3.ZERO
dir.x = sign(player.transform.origin.x - transform.origin.x)
dir.z = sign(player.transform.origin.z - transform.origin.z)
dir = dir.rotated(Vector3.UP, cam.rotation.y)
var direction = dir.dot(transform.origin)
if direction > 0:
sprite.set_flip_h(false)
elif direction < 0:
sprite.set_flip_h(true)
"cam" references the camera and "sprite" references the AnimatedSprite3D
I'm sure there's an obvious explanation that I could figure out with more tinkering, but I thought I'd ask for help because understanding this problem should help me understand 3D better in the long run. I read the documentation - it's gotten me this far, but I'm stumped as of right now. Any advice is greatly appreciated. Thanks!