0 votes

This might be kind of a silly question, but it was an idle thought I had while working on some weapon ideas. I know that when you instance a projectile in a 2D game, typically you'll use set as toplevel(true) or some other method so it moves independently of the player.

But what if you DON'T want that to happen? Say, for example, you have something like a spinning blade weapon that spawns when you press fire and you want it to stick to the player (so they can run into enemies) until its timer expires and it despawns. (It's technically a "projectile" I guess, even though it has no velocity.)

From my testing, it seems like if you omit setting it as toplevel it will move along with the player just fine, but also spawns at random all over the place. I'm guessing there must be some way to set it to stay at the Position2D it spawns from and move along with the player character but it has eluded me so far.

I know it's kind of an unusual question, but it might also come in handy for someone else trying to debug weird spawning behavior. Thanks for being so helpful!

Godot version 3.5
in Engine by (20 points)
edited by

2 Answers

+1 vote

Have you tried spawning it as a child of the player you want it to stick to? and if it's not spawning at the position you want, try setting the position before you add it to the scene

by (709 points)

I checked and they are children of the Player object. I think I figured it out though, in case someone else runs into a similar problem down the road.

In my shooting code, I had set bullet.transform = origin.global_transform. "Bullet" is a packed projectile scene and "origin" is a Position2D used as the gun's muzzle. This worked fine as long as I was setting projectiles to top level.

To solve the problem I was having, I wound up changing it to bullet.global_transform = origin.global_transform and it works perfectly for what I was trying to do.

Interestingly, you get the same issue of wonky spawns if you set toplevel as true after that change. I think I really need to read up on how transforms and coordinates work in more detail.

+1 vote

How I solved this is defined two type of projectiles:

  • Stage projectiles
  • Player projectiles

Stage projectiles are the standard projectile like a street fighter haduken. They spawn on the stage, and they are relatively independent from the player who spawned it. The only link it has to the player is when it initially spawns, the position is relative to the player but the projectile is added as a child to the node representing the stage. That way, if the player (also a child of the stage) moves, the projectile isn't affected

Player projectiles are specials projectiles that will stick to the player. This is achieved by making the projectile a child of the player's node. In this case, if the player moves, the projectiles also moves with the player. An example of this type of projectile is the leaf attack of mega man from Super Smash Bros 4, where the leaves spin around mega man and act as a shield (he can then throw the leaves, which become Stage projectiles, since they lose their positional link to the player.

What both types of projectiles should have in common is a) they have an initial spawn point that is likely relative to the player/creature spawning it; and b) is from their point view, it doesn't matter who their parent is; they can have their own movement animation without having to worry about the parent's movement.

For example, if the SSB4 mega man leaf projectile example above were a stage projectile, since the projectile doesn't move at all in relation to megaman, when on the stage the leaves would simply be stationary and not move, even if megaman moves. In the example of the SF haduken, if it were a player projectile, if Ken/Ryu shoot this type of haduken, when they remain stationary, the haduken will appear to travel normally across the stage, but if ken/ryu back up, the projectile will also backup at the same speed even if it's traveling forward (and it will relatively speed up if the player walks forward) since it's anchored/linked to the player.

Therefore, you can have 1 type of projectile scene, and the only thing that changes is whether it is a child node of the player or stage, and that's how you can have many types of projectiles that are sticking to the player or independent to the stage.

by (434 points)

Thanks! That's a fantastic and in depth answer. I honestly never really considered categorizing them into Stage and Player projectiles.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.