我怎样才能让敌人在戈多中产生并被吸引/移动到一个静止点?

问题描述

我在使用 path2D 时遇到问题。所以敌人会根据“你的第一场比赛”每隔一段时间产生。但是,我想要的是让它们从它们生成的任何地方生成并移向/移动到固定角色或一组坐标。

我尝试了很多东西,但无法让它发挥作用。有什么想法吗?

代码将不胜感激。

(如果这有什么不同,玩家将使用重心来吸引他们路径上的敌人 - 我是否需要定义一条新路径,以便他们不会立即继续返回中心,但之后他们会在某个时间?)

func _on_MobTimer_timeout():
    $Path2D/PathFollow2D.offset = randi()
    var mob = Enemy.instance()
    add_child(mob)
    var direction = $Path2D/PathFollow2D.rotation + PI / 2
    mob.position = $Path2D/PathFollow2D.position
    mob.linear_veLocity = Vector2(rand_range(mob.min_speed,mob.max_speed),0)
    mob.linear_veLocity = mob.linear_veLocity.rotated(direction)

谢谢。

解决方法

因为你不需要寻路,所以我可能会这样做:
在你的游戏脚本中有类似的东西:

onready var player := $Player

func _on_SpawnEnemyTimer_timeout():
    var enemy = Enemy.instance() as Enemy
    enemy.player = player
    add_child(enemy)

在你的敌人脚本中有类似的东西:

export var speed := 10.0

# you can assign this when you spawn your enemies
var player: Node2D

func _physics_process(delta: float):
    add_central_force(global_position.direction_to(player.global_position) * speed)

那行得通吗?