+1 vote

I am making a doom-like first person shooter in 3d world. I am showing enemies as a 2d sprite animated with spritesheet. So i need to determine where the player is regarding to the object.

So if player if front of the object, i will show the front facing sprite animations.
If player is on the right of the object, i will show the right spirtes.
etc

I have followed a video where it is implemented with the camera like this:

var player_forward_direction = -camera.global_transform.basis.z
var npc_forward_direction = global_transform.basis.z
var npc_left_direction = global_transform.basis.x

var left_dot = npc_left_direction.dot(player_forward_direction)
var forward_dot = npc_forward_direction.dot(player_forward_direction)

But the problem here is that if i stand in one place and rotate the camera, this code is impacted. I need to implement this based only on player position, not on camera rotation.

Please help i am total noob in 3d math.

in Engine by (64 points)

1 Answer

0 votes

try this code:

var enemy_translation=enemy.translation#replace "enemy" with enemy the node
var player_translation=player.translation#replace "player" with player the node
var d #direction to player


var dx
var dz
if enemy_translation[0]<player_translation[0]:
    dx="right"
    #enemy is to the right of the player
if enemy_translation[0]>player_translation[0]:
    dx="left"
    #enemy is to the left of the player

if enemy_translation[2]>player_translation[2]:
    dz="front"
    #enemy is in front of the player
if enemy_translation[2]<player_translation[2]:
    dz="back"
    #enemy is behind the player

if abs(enemy_translation[0]-player_translation[0])>abs(enemy_translation[2]-player_translation[2]):
    d=dx
else:
    d=dz

if d=="left":
    pass #put the correct sprite
if d=="right":
    pass #put the correct sprite
if d=="front":
    pass #put the correct sprite
if d=="back":
    pass #put the correct sprite

I hope that works, I can't test it very well.
if something is backwards that flip the relative ">" and "<" symbols.
if it doesn't work, I can try to help you get it working, but I am new pretty new to this stuff too. :)

by (16 points)
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.

Categories