Help with raycast2d position

Godot Version



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)

do you have a Node class type node as a parent of this raycast2d node?
if yes, change it to Node2D type

The raycast node is a child of the Character body 2d node

is this only once when needed or you put it in _process something

the raycast should follow the characterbody2d’s position changes movement, unless you do something like enabling Top Level
on your raycast, then it wont follow

1 Like

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?

Top level isn’t enabled but it still doesn’t work. Even when I debug, the raycast position doesn’t change

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

Oh okay. How do I add it please?

how your code is looking now to set these up?


And here’s the code for the main game

Here’s the code for the player


i assume your marker node is the raycast?
where did you set up your raycast? i only see Player Input, Fire scene being instantiated and added child

where is this code:

if the marker is your raycast, then
change this line

fire.global_position= $player.global_position + $player/marker.target_position

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

1 Like

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

fire.global_position = $player.global_position + $player/marker.target_position

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.

fire.global_position = $player/marker.global_position

If you want to flip it you could scale the marker’s x position by -1

func flip_marker() -> void:
    $player/marker.position.x *= -1

# some helper functions for getting/setting
func is_marker_flipped() -> bool:
    return $player/marker.position < 0

func set_marker_flipped(flipped: bool) -> void:
    if is_marker_flipped() != flipped:
1 Like

Thank you both for help. Really appreciate it ^^