The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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:
setup
mob_generation() func gameover() -> void:
get
mobtimer().stop()
get
score_timer().stop() func newgame() -> void:
score = 0
get
player().start(getstartingposition().position)
getstarttimer().start() func spawnmobalongpath() -> void:
var mob: Mob = mob
scene.instance()
# choose a random point along the spawn path
var spawnlocation = getmobspawnlocation()
spawnlocation.offset = randi()
print
debug("Sampled spawn location:", spawnlocation.position)
# Rotate 90 degrees + some randomness +- 45
var mob
direction = spawnlocation.rotation + PI / 2
mob
direction += randrange(-PI/4, PI/4)
# Pick mob velocity
var speed = rand
range(150.0, 250.0)
var velocity = Vector2(speed, 0).rotated(mobdirection)
# Apply generated values
mob.position = spawn
location.position
mob.rotation = mobdirection
mob.linear
velocity = velocity
addchild(mob)
print
debug("Spawned a mob at", mob.position, "with velocity", velocity) func onMobTimertimeout():
spawn
mobalongpath() func onScoreTimer_timeout() -> void:
score += 1 func onStartTimertimeout():
get
mobtimer().start()
get
score_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:spawn
mobalongpath()
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:spawn
mobalongpath()
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:spawn
mobalongpath()
<...>

Here is the Path2D in editor:
enter image description here

Here is the Main scene tree structure:
enter image description here

Godot version v3.4.4.stable.official [419e713a2]
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.