Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | morningkingdom |
Hi there,
On My Player have Area node and it can detect the enemies that
Entered inside.
What im Trying to Do is, lock to target (Player face the target and stayy locket Even if itself move) The more close one that insade the Area.
But My enemies Get instance at the main scene more then once, So i did use a group
Named “enemy” to Get enemies.
But still did not work.
I did work on vector.distance_to and look_at to solve this issue and Failed To Do So.
So i need help, same or diffrent kind of view and an example syntax.
Thank you, and sory for Bad gramer.
HI morningkingdom.
Can you share the code that you have now so we can see if we can find the problem?
This question has a working example.
If you have multiple enemies and you need to pick the closest, you will need the distance to each of them. You can use a Vector2 (or Vector for 3d) = player.position - enemy.position and call the length method on that vector to get the distance, then pick the closest.
AndyCampbell | 2020-11-12 20:48
Thanks for anwser, well because of a less busy “day job” i had today, i thought about this problem, and kind of solve it, this is the code i came up with, this code is on the player node:
func _on_Enemy_Detect_body_entered(body):
body = enemy1
var min_distance = 999
var lock_enemy
var instanced_enemies = get_tree().get_nodes_in_group("enemy")
if body == enemy1:
print ("enemy entered the area")
for close_enemy in instanced_enemies:
var distance = translation.distance_to(close_enemy.translation)
if (distance < min_distance):
lock_enemy = close_enemy
print ("close enemy : " +str(lock_enemy), "and dinstance from it: " +str(distance))
look_at(lock_enemy.translation,Vector3.UP)
else:
enemy1 = null
this is the output:
well, i think i got the closest enemy but things got messier when it came to do lock the enemy part, because what i wanna do is, this :
(i have great paint skils)
well im new at geme dev, and codding, so i need help guys. what should i do with this?
morningkingdom | 2020-11-13 17:54