Raycast2D node is placed globally

Godot Version

v4.2.1.stable.mono.official

Question

Hey guys,
I am currently trying to get into Raycast2D nodes for detecting walls for a platformer character, but despite all the tutorials I did not get it working. Finally I found out that the raycasts - which are basically children of my player node (player node is saved into a seperate *.tscn file) - are placed globally always on the same position although it should change as soon as my character is walking. That way my raycasts do not change their value even if I walk.

Due to the fact that Raycast_Right always shows true while Raycast_Left always shows false leads me to the suspicion that the Raycasts are placed at world origin and the right one detects my Tilemap (starting at 0,0) while the leftone will never detects anything.

Now I try to find out what I am doing wrong but this seems to be a very specific problem, so I could not find anything on stackoverflow, reddit and here. Are the raycasts really placed globally or is raycast.global_position showing my only the position in its owning scene? But why is there then no change when player is running/jumping, whatever? Should I change from the raycast2d nodes (which are very convenient with their visual representation) to code-based casts?

Thanks a lot in advance!!
Jensolo

I used the following command to check the global position inside the StateMachine of my player:

print(raycast_left_bottom.global_position)

I used this code to do the raycast:

print(raycast_left_top.enabled)

My nodes are structured like this:

World.tscn:

- (World)[Node2D]
    -(Player)[Player.tscn]
    -(Level)[Node]
        -(TileMap)[TileMap]

Player.tscn

(Player)[CharacterBody2D]
    - (AnimatedSprite)[AnimatedSprite2D]
    - (Raycasts)[Node]
        - (Raycast)[Raycast2D]
    - (StateMachine)[Node with Script]
        - (States)[Nodes with Scripts]

Edit:
Additional infos:
Raycasts were set to:
Enabled = On
Exclude Parent = On
Hit from Inside = Off
Collide with Areas and bodies

Edit 2
Now I found the button to show the collision shapes and indeed my theory was right. While the player is in the lower right corner (blue alien with glasshead) the raycast is placed in the upper left corner (red arrow), see attached raycast_level.png

In player.tscn it is placed as seen in file raycast_playertscn.png

raycast_level.png

raycast_playertscn.png

this should be the reason why it placed globally, change it to Node2D

Incredible… you are gorgeous :slight_smile: That solved the problem I wasted 2 hours now instantly! Thanks a lot!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.