Help with movement script

Godot Version

godot 4.7.2.stable

Question

I want my player to flip and move the opposite way when the raycast detects a certain collision like maybe that has a group or something

extends CharacterBody2D

@onready var raycast = $RayCast2D

const FLAP_POWER = -300.0
const MOVE_SPEED = 180.0

var flipped = false

func _onready():
	flipped = false

func _process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta
		
	if Input.is_action_just_pressed("Flap") and not is_on_floor():
		velocity.y = FLAP_POWER
	
	if flipped == false:
		velocity.x = MOVE_SPEED
	elif flipped == true:
		velocity.x = -MOVE_SPEED
		
	if raycast.is_colliding() and flipped == false:
		flipped = true
	elif raycast.is_colliding() and flipped == true:
		flipped = false
	
	move_and_slide()

Who flips the raycast?

i have not got to the part but the flipped variable is for the player movement

Yeah but if you don’t flip it as soon as it detects the obstacle, it will continue to detect the same obstacle and ping-pong the filipped state.

then how should i do it?

Flip the raycast whenever you flip the velocity.

i got it to work :slight_smile: