Hi
I’d like to make a character shoot projectiles, so I attached a raycast2d node to the characterbody2d and in the main game, I set the projectile position to be at the raycast target_position. Problem is that the raycast2d position doesn’t seem to move with the player. I tried using the print() method to print out the target_position, but even after I move the player, the position stays the same. Any help would be appreciated and if any extra information is needed, I’ll make sure to share(I’m on my phone right now and can’t really attach code, but will if it’s required)
I set the projectile position to be at the raycast target_position
Do you mean like this?
projectile.position = $RayCast2D.target_position
This would place the projectile near the origin, since target_position does not update relative to position. You will have to add the ray cast’s global_position or the player’s position to it, though this is all a very strange way to use raycasts; could you explain why you are spawning projectiles at the target_position instead of a marker or offset from position?
I tried looking up tutorials for specifically marked 2d but no luck on that so I just read the documentation and there isn’t much for me to work with there.
fire.position = $player/marker.global_position
The fire here is the projectile but it doesn’t spawn at all. It does when I use >position< instead of >global_position< but the coordinates for >position< seem to be constant
For additional context: it’s not a normal projectile, it’s an area2d node that spawns in front of the player while their holding down the shoot button
of course it will never move, because the target position is locally set position, even your character move, the target position will stay the same
what you need to do is to add the player’s global position to the target position of the raycast so it will move
Thank you so much, it works now. Really appreciate your help. Would love an explanation as to why I needed to add player’s global position AND raycast’s position together, because I don’t really get. Thanks again
target position of a raycast is a set local position from the raycast local position itself
if you just use raycast’s target position, then it will always tell you the same value because it’s a set local position from raycast position
since we know the target_position is a local position extended from raycast position, and we need to calculate the real global position of that target position, then we will need the player global position added with this extended local position of the raycast target position
i do think using the raycast global position also will work too, but it looks like you put the raycast exactly Vector2(0,0) position as the child of the player, hence it works for this
Hi. Anyway you could show me how to use offset like you said? I got it sorta working with raycast but I don’t think this is the most convenient way to do it because now I have another issue of having to flip the projectile when the characterbody2d is facing a different direction
this recommendation from @zdrmlpzdrmlp is exactly what I was talking about, this is using both of my recommendations, but you will probably get the same effect from just the marker’s global_position. Again it is strange for this to be a raycast, changing it to a Node2D or Marker2D would make more sense.