Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Nyx |
I’m currently following the “Your first game” tutorial, and the way they instruct it, they’d have you assign the same hitbox to every mob despite the fact that one in particular is wildly different.
So first I tried assigning unique collisionshape2d nodes to each animatedsprite node, but then of course I found out that collisionshape2d nodes can only be children of rigidbody2d or area2d nodes.
My second idea was to give each mob its own scene, and that worked fine. I currently have a scene for every mob where they all have their own collision and animations, and that works fine, but it brings a new issue where I can’t just choose a random animatedsprite in the one mob scene because, again, I split the mobs into individual scenes.
So my question is twofold. First, is there a better way to assign multiple sprites different hitboxes while keeping everything else the same?
Second, if I am going down the right path, how can I have Godot randomly pick between the three scenes I’ve created for my mobs?
I can’t answer your first question so I’ll address your second in a comment.
Godot has a built-in random number generator named (unsurprisingly) RandomNumberGenerator
. All you need to do is create an instance of it with .new()
, and then call .randomize()
on the instance once. After that, you can call .randi_range(0, 3)
to get a random number. Combine that with a match
block to determine which scene is instanced depending on the generated number. You can read more about match
here. It’s basically a more elegant, readable version of a long if/else block.
DDoop | 2020-07-19 19:44