Topic was automatically imported from the old Question2Answer platform.
Asked By
TheMadScientist1234
What I mean by this is the rotation_degrees of the sprite will snap to 0, 90, 180, 270 depending on what value I want to set it to. The goal is to have an enemy look_at(player.position) and yet it stays rotated at 90 degree intervals.
This might not be the most efficient way, but it should work:
#These are your snap vectors
var up = Vector2(0,-1)
var down = Vector2(0,1)
var left = Vector2(-1,0)
var right = Vector2(1,0)
#This will give you the vector to the player
var look_vector = player.position - enemy.position
#The take the dot product of this vector to each of the snap vectors.
#The one with the biggest dot product is the "most parallel".
var snap_vector = up
var max_dot_product = look_vector.dot(up)
var dot_product = look_vector.dot(down)
if dot_product > max_dot_product:
max_dot_product = dot_product
snap_vector = down
dot_product = look_vector.dot(right)
if dot_product > max_dot_product:
max_dot_product = dot_product
snap_vector = right
dot_product = look_vector.dot(left)
if dot_product > max_dot_product:
max_dot_product = dot_product
snap_vector = left
#You will end up with the closest snap_vector at the end