I have a question regarding the official tutorial

I have a question regarding the official tutorial. Could someone please help me with it?

我有一个关于官方教程的问题,请大家帮我解决

Godot Version

4.6.1

Question

我在跟随官方文档进行学习,链接在这里: 游戏主场景 — Godot Engine (4.x) 简体中文文档

我完全按照文档内容编写,但是在敌人随机移动的部分我的效果与预期不符

敌人生成时总会向上方移动,且速度很快。我不理解,但我和文档的内容一致,至少我觉得

随意请大家帮我看一下,具体的代码我贴到下方

I am studying according to the official documentation. The link is provided here. 游戏主场景 — Godot Engine (4.x) 简体中文文档

I wrote it completely according to the content of the document, but in the part where the enemies move randomly, my effect did not match the expectation.
When enemies are generated, they always move upwards and at a fast speed. I don’t understand it, but it’s consistent with the content of the document. At least, I think so.
Please feel free to take a look at it for me. The actual code is posted below.

func _on_mob_timer_timeout() → void:

Create a new instance of the Mob scene.

var mob = mob_scene.instantiate()

# Choose a random location on Path2D.
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()

# Set the mob's position to the random location.
mob.position = mob_spawn_location.position

# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2

# Add some randomness to the direction.
#direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction

# Choose the velocity for the mob.
#var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)

# Spawn the mob by adding it to the Main scene.
add_child(mob)

What does your path look like?

It should be drawn clockwise.

And if your path has only 2 points, they will spawn only on that line.
If it is a line and drawn counterclockwise they will move upwards.

And make sure the mob has gravity scale set the 0.
That’s everything I can think of right now. I tested the code and it seems fine.

1 Like

There shouldn’t be a # at the beginning of that line. # is used to mark comments, any code behind it won’t get executed.

1 Like