RayCasting 2D Question?

Godot Version

4.2.2

Question

Hello all,

I my continuing attempt to get a wallslide/jump thing happening in my 2D game I have had to switch to using a RayCasting method because the is_on_wall() isn’t very satisfactory.

So, my question is, if you flip the sprite that has a RayCasting2D node on it does the RayCast flip with it or does it continually face the in one direction. If not, is there any possible way in gdscript to flip the RayCast when the sprite does changes direction?

Thankyou and Regards.

Ok,well, I never would’ve come up with that on my own :stuck_out_tongue:. I also didn’t know you could run a bool check on flip_h - that took me by surprise. I won’t need it but good to know.

In fact, there was nowhere on the internet that I came up with any solution.

Thankyou so much :sunglasses:.

I tried the code you posted in this way:

if direction > 0:
		hero_sprite.flip_h = false;
		wallchecker.cast_to = wallchecker.cast_to.rotated(PI)   #facing right - rotate accordingly
	elif direction < 0:
		hero_sprite.flip_h = true;
		wallchecker.cast_to = wallchecker.cast_to.rotated(PI) ; #facing left rotate 180

With this to define the wallchecker variable:

## Raycast 2D
@onready var wallchecker = $Wallchecker

And this was the result when I ran the game:

And I really don’t know how to solve it or why is was caused. Do you have any ideas? Line 185 is in error this was an older screenshot.

You can also modify the Scale property of your Raycast to achieve the same effect. Keeping the Y value unchanged, but change the Scale’s X between 1 or -1 depending on which way your character is facing.

1 Like

That seems to have done the trick :grinning:.

No errors and getting a true result whenever near a wall on both sides. I’d say that’s a success :sunglasses:.

Thankyou for responding!