How do I make an enemy jump out of my boss

Godot Version

4.4.1

Question

I’m making a boss that has an attack that makes enemies jump out of it in a random range. I know how to spawn enemies but I don’t know how to spawn them jumping out of a moving 2dnode. Thanks for the help.

Can you apply the same spawning script with a velocity-on-start twist?

1 Like

My first question is how do I do that? My second question is would I have to make a new or child node because the boss would be on a specific scene.

You can call add_sibling() from the boss to add a mob to the scene at the same level of the node hierarchy as the boss. Something like:

# Untested! Assumes this is in the boss script...

const LAUNCH_VELOCITY: float = 10.0

func rand_vel() -> float:
    var vel: float = Vector2.FORWARD       # (1.0, 0.0)
    var ang: float = randf_range(0.0, TAU) # Random angle in a full circle.
    vel *= LAUNCH_VELOCITY                 # Scale it to length.
    return vel.rotated(ang)                # Apply the rotation.

func spawn_minion() -> void:
    var minion = preload("res://evil_critter.tscn").instantiate()
    minion.velocity = velocity + rand_vel() # Boss velocity + random.
    add_sibling(minion)

You can do a lot more with this (and you can also tighten it up a bunch; I spread it out a bit to make it easier to see what the steps are), but something like this ought to work.

Edit: “FORWARD”, not “FOREWARD”…

2 Likes

how do I know their in the the same hierarchy?

If you’re calling add_sibling() in the boss script, the node will be added as a child of the boss’s parent node. If you want it to be a child of the boss, you can add_child() instead.

Alternately, you could do something like:

$/root/Game/Room/Monsters.add_child(minion)

Or whatever fits your layout.

1 Like

I put in the code it says cannot find member “FORWARD” in base “Vector2”. is it because I didn’t make a function? Also the bosses minions are called slime should I replace the minions with slime for it ti work?

Ah, FORWARD is for Vector3. Try DOWN. This is why I put the “untested” comment in. :slight_smile:

As for the naming, I just made up example names. Call it whatever you like; if “slime” is a better fit than “evil_critter”, by all means name it “slime”.

1 Like

sorry to bother you but now it says Cannot assign a value of type “Vector2” as “float”. Thanks for all the help

they ment to type it Vector2

var val: Vector2 = Vector2.DOWN
2 Likes

What about the angle? Should it stay float?

Do you have an error with angle? randf_range returns a float, so the variable should expect a float.

1 Like

it says cannot return value of type “Vector2” because the function return type is “float” even though I did var vel: Vector2 = Vector2.DOWN

Change the function return type to Vector2

func rand_vel() -> Vector2:
1 Like

should I change the launch velocity to vector2 too?

Do you have an error? It looks ok to me

1 Like

yea it says identifier “velocity” not declared in current scope and highlights minion.velocity = velocity + rand_vel()

Does your script extend CharacterBody2D? Is it attached to your boss or something else? If you do not want to include the boss’ velocity you can remove it from the equation so it only takes rand_vel()

1 Like

I don’t see any minions coming out, is it because my boss is to big? Or is it because I attached the wrong preload because my minions have code of their own.

How did you connect the spawn minion function? Could you share your scene tree?

1 Like