Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | andriikhomiak |
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 setup_mob_generation() -> void:
randomize()
func get_player() -> Player:
return $Player as Player
func get_score_timer() -> Timer:
return $ScoreTimer as Timer
func get_start_timer() -> Timer:
return $StartTimer as Timer
func get_mob_timer() -> Timer:
return $MobTimer as Timer
func get_starting_position() -> Position2D:
return $StartingPosition as Position2D
func get_mob_spawn_location() -> PathFollow2D:
return $MobPath/MobSpawnLocation as PathFollow2D
func _ready() -> void:
setup_mob_generation()
func game_over() -> void:
get_mob_timer().stop()
get_score_timer().stop()
func new_game() -> void:
score = 0
get_player().start(get_starting_position().position)
get_start_timer().start()
func spawn_mob_along_path() -> void:
var mob: Mob = mob_scene.instance()
# choose a random point along the spawn path
var spawn_location = get_mob_spawn_location()
spawn_location.offset = randi()
print_debug("Sampled spawn location:", spawn_location.position)
# Rotate 90 degrees + some randomness +- 45
var mob_direction = spawn_location.rotation + PI / 2
mob_direction += rand_range(-PI/4, PI/4)
# Pick mob velocity
var speed = rand_range(150.0, 250.0)
var velocity = Vector2(speed, 0).rotated(mob_direction)
# Apply generated values
mob.position = spawn_location.position
mob.rotation = mob_direction
mob.linear_velocity = velocity
add_child(mob)
print_debug("Spawned a mob at", mob.position, "with velocity", velocity)
func _on_MobTimer_timeout():
spawn_mob_along_path()
func _on_ScoreTimer_timeout() -> void:
score += 1
func _on_StartTimer_timeout():
get_mob_timer().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:spawn_mob_along_path()
Spawned a mob at(0, 0)with velocity(80.529663, 127.630013)
At: res://scenes/main/Main.gd:60:spawn_mob_along_path()
Sampled spawn location:(0, 0)
At: res://scenes/main/Main.gd:48:spawn_mob_along_path()
Spawned a mob at(0, 0)with velocity(105.036392, 113.644714)
At: res://scenes/main/Main.gd:60:spawn_mob_along_path()
Sampled spawn location:(0, 0)
At: res://scenes/main/Main.gd:48:spawn_mob_along_path()
Spawned a mob at(0, 0)with velocity(-159.286285, 165.168213)
At: res://scenes/main/Main.gd:60:spawn_mob_along_path()
<...>
Here is the Path2D in editor:
Here is the Main scene tree structure: