Hello!
I wanted to add drops to a node which is instancing them, and i’m not sure how to scatter them in different directions so the player has to go pick them up.
I’m only a month new and i’ve tried to follow tutorials on magnets and item drops but it is rather specific and really complicated.
How an object moves depends on what type of object it is; If your drops are RigidBody2D you could apply a randomized linear_velocity right after it is instantiated. A CharacterBody2D needs velocity, and everything else should do fine with position changes over time.
In the latter two cases you will need a script on your drops to control the movement, something like so:
# drop script
var direction: Vector2
const dampening: float = 100
func _process(delta: float) -> void:
position += direction * delta
direction = direction.move_toward(Vector2.ZERO, dampening * delta)
Thank you!
By what you mean by drop script, do you mean the node that is dropping the object?
Also I have a marker which determines the location of the node that is dropping it, will the velocity do anything to the drop point?
The game is also a top down, so does the randf drop in 360 deg of the object? I’m unsure of the calculations between xy and radians…
Drop script would be the object being dropped, maybe we need a better name for those like “Collectables”
For a 360 degree approach try this instead:
# spawning/dropping collectables script
var clone = drop_prefab.instantiate()
var random_angle = randf_range(0, TAU)
var random_force = randf_range(200, 1200)
var x = cos(random_angle) * random_force
var y = sin(random_angle) * random_force
clone.direction = Vector2(x, y)
add_child(clone)
Thank you once again!
One more question, the collectable itself doesn’t handle instanciation. The Node from a different scene does. So does that mean I would still use the “var clone” on the object itself since it would instantiate itself?
Basically the Parent object (lets say the spawner) has the condition that upon a button being pressed and detecting the player, it spawns the collectable as a child.
and the call for the spawner to get the collectable is get_parent().add_child(collectable)
Sorry if i’m horribly misunderstanding this, but currently it won’t assign it on a “packedscene”
No my sample here is assumed to be on your spawner node. You didn’t post any code snippets so I had to make it up. This is not for the collectable object itself.
You probably have instantiate() somewhere in your spawner script, and a variable to keep it’s result, use that as a guide or paste it and I can help further.