I can't turn the RayCast2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Douglas Levita

I’m still studying the godot scripts and I’m having a problem with the RayCast, I can’t make it turn in the x direction along with the character. This is my code, I know it’s very amateur, but I’m still new. can anybody help me?`

func get_input():
var left = Input.is_action_pressed(“esquerda”)
var right = Input.is_action_pressed(“direita”)

if left:
	velocity.x -= run_speed
	Sprite.flip_h = true
	Vector2.RayCast2D(0, -1)

elif right:
	velocity.x += run_speed
	Sprite.flip_h = false
if not is_on_floor():



:bust_in_silhouette: Reply From: SQBX

Delete this line

Vector2.RayCast2D(0, -1)

And at the bottom of the function, write

$RayCast2D.cast_to(Vector2(-1, 0) if velocity.x < 0 else Vector2(1, 0))

You may need to tweak the -1 and 1 to make theRayCast2D longer.

Also, if your RayCast2D is not named RayCast2D in your tree, change $RayCast2D to whatever the name of the RayCast2D is named prefixed by a $.


$WhateverYourRayCastIsNamed.cast_to(Vector2(-1, 0) if velocity.x < 0 else Vector2(1, 0))

Thank you very much

Douglas Levita | 2023-01-03 15:21

You’re welcome! If you’d like, I’d appreciate it if you marked my answer as “Best Answer” (the checkmark button to the left). Thank you :slight_smile:

SQBX | 2023-01-03 15:25