0 votes

My problem is mainly having NPC's changing 2D sprites in a 3D space. What I want to obtain is how the NPC's look in The Elder Scrolls I & II and have their sprites change depending on where i'm looking at them from. All I can think of is using a raycast on my player, but I'm fairly new to godot and programming and don't quite understand how well I'd be able to do that. Anything helps, thank you.

in Engine by (11 points)

do you mean you want the sprites to face you? I think you could use "look_at()"


1 Answer

0 votes

Assuming that your Player is facing its z-axis, and you know your Player's and NPC's position, you could calculate the angle like this:

var facing = transform.basis.z
var player_enemy_direction = player_pos.direction_to(npc_pos)
var direction_scalar = player_enemy_direction.dot(facing);

Where palyer_pos and npc_pos are global positions and they are normalized.

Based on the direction_scalar, you can change your NPC's sprite.
The direction_scalar will be a number between -1 (180°) and 1 (0°) (if your positions are normalized)

It's quite tricky. I would recommend you to check out Godot's Vector math documentation

by (148 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.