Getting facing direction of a KinematicBody2D

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

I’m making a top down shooter where enemies have a field of vision. If the player enters the FOV of the enemy, the enemy runs to the player. I’m having trouble with making the FOV work properly. The way my FOV system is supposed to work is the enemy uses the dot product to figure out if the player is in front of the enemy. If yes, then the enemy shoots a ray towards the player to check if there isn’t anything blocking the view to the player. My problem is I can’t figure out how to get the facing direction of the enemy that is supposed to be used to figure if the player is in front of the enemy in the dot product. I have tried a lot of things and nothing works so far.

:bust_in_silhouette: Reply From: klaas

Hi,
you mean the the orientation vector of the KinematicBody?

var forward_vector = Vector2(0,1)
var the_object = self
var orientation_global = (the_object.to_global( forward_vector ) - the_object.global_position)

i make a vector pointing one unit to the front of the object and transform them to global space. Then subtract the offset (object position). Then you have a vector from world origin one unit in facing direction,

This worked for me, thank you.

o k | 2020-08-30 09:56