I'm following the tutorial on dodge the creeps, and unable to make mobs spawn randomly on the PathFollow2D - looks like the position that is being returned by the PathFollow2D is the one set in the editor > MobPath > Node > Node2D > Transform > Position (changing it here makes creeps spawn at this location instead of the top left corner). Regardless of the offset or even the shape of the curve (see screenshot), mobs spawn only at (0, 0)
My Main.gd:
extends Node
const Player = preload("res://scenes/player/Player.gd")
const Mob = preload("res://scenes/mob/Mob.gd")
export var mob_scene: PackedScene
var score: int
func setupmobgeneration() -> void:
randomize()
func get_player() -> Player:
return $Player as Player
func getscoretimer() -> Timer:
return $ScoreTimer as Timer
func getstarttimer() -> Timer:
return $StartTimer as Timer
func getmobtimer() -> Timer:
return $MobTimer as Timer
func getstartingposition() -> Position2D:
return $StartingPosition as Position2D
func getmobspawn_location() -> PathFollow2D:
return $MobPath/MobSpawnLocation as PathFollow2D
func ready() -> void:
setupmob_generation()
func gameover() -> void:
getmobtimer().stop()
getscore_timer().stop()
func newgame() -> void:
score = 0
getplayer().start(getstartingposition().position)
getstarttimer().start()
func spawnmobalongpath() -> void:
var mob: Mob = mobscene.instance()
# choose a random point along the spawn path
var spawnlocation = getmobspawnlocation()
spawnlocation.offset = randi()
printdebug("Sampled spawn location:", spawnlocation.position)
# Rotate 90 degrees + some randomness +- 45
var mobdirection = spawnlocation.rotation + PI / 2
mobdirection += randrange(-PI/4, PI/4)
# Pick mob velocity
var speed = randrange(150.0, 250.0)
var velocity = Vector2(speed, 0).rotated(mobdirection)
# Apply generated values
mob.position = spawnlocation.position
mob.rotation = mobdirection
mob.linearvelocity = velocity
addchild(mob)
printdebug("Spawned a mob at", mob.position, "with velocity", velocity)
func onMobTimertimeout():
spawnmobalongpath()
func onScoreTimer_timeout() -> void:
score += 1
func onStartTimertimeout():
getmobtimer().start()
getscore_timer().start()
Example logs from running this scene:
Godot Engine v3.4.4.stable.official.419e713a2 - https://godotengine.org
OpenGL ES 2.0 Renderer: AMD RENOIR (DRM 3.44.0, 5.17.9-051709-generic, LLVM 12.0.0)
OpenGL ES Batching: ON
Sampled spawn location:(0, 0)
At: res://scenes/main/Main.gd:48:spawnmobalongpath()
Spawned a mob at(0, 0)with velocity(80.529663, 127.630013)
At: res://scenes/main/Main.gd:60:spawnmobalongpath()
Sampled spawn location:(0, 0)
At: res://scenes/main/Main.gd:48:spawnmobalongpath()
Spawned a mob at(0, 0)with velocity(105.036392, 113.644714)
At: res://scenes/main/Main.gd:60:spawnmobalongpath()
Sampled spawn location:(0, 0)
At: res://scenes/main/Main.gd:48:spawnmobalongpath()
Spawned a mob at(0, 0)with velocity(-159.286285, 165.168213)
At: res://scenes/main/Main.gd:60:spawnmobalongpath()
<...>
Here is the Path2D in editor:

Here is the Main scene tree structure:
