Stairs in a topdown

Godot Version

4.2.1

Question

So for more than a month now I have been trying to get this right and now I have the right Code for staits to work.
func _process(delta)
if Input.is_action_pressed(“ui_left”):
position.y+= delta * 100
if Input.is_action_pressed(“ui_right”):
position.y-= delta * 100

the problem is I have no idea how to make this work in the area2d.
Screenshot 2024-03-02 101158
I add the _on_area2d_body_entered signal but then I cant use _process(delta). does anyone have an idea?

and btw this is the code for character movement:
@export var speed: int = 100
var direction=Vector2.ZERO
func handlInput():
direction=Input.get_vector(“ui_left”,“ui_right”,“ui_up”,“ui_down”)
velocity = direction*speed

func _physics_process(delta):
handlInput()
move_and_slide()

You could have an var on_stairs = false variable, and use _on_area2d_body_entered to set it to true and _on_area2d_body_exited to set it to false, so you’d know when the character is using stairs.
Then in the movement code:

if on_stairs:
   if direction < 0:
       position.y+= delta * 100
   elif 0 < direction:
       position.y-= delta * 100

Or I guess setting velocity.y instead of position.y might be better.

1 Like

but then we arent using _process(delta) so we cant use delta in equations

you can pass delta as a parameter to handlInput(delta)

1 Like

I have just started working with godot… can you write in code mate?

var on_stairs = false
func _process(delta):
if on_stairs:
if Input.is_action_pressed(“ui_left”):
velocity.y+=delta * 100
elif Input.is_action_pressed(“ui_right”):
velocity.y-= delta * 100

func _on_peleh_body_entered(body):
on_stairs=true

I did this and still nothing. the direction code you used made the game crash on the click of start so I still use Input.is_action_pressed(“ui_left”) instead

@export var speed: int = 100
var direction=Vector2.ZERO
var on_stairs = false

func handlInput(delta):
    direction=Input.get_vector(“ui_left”,“ui_right”,“ui_up”,“ui_down”)
    velocity = direction*speed

    if on_stairs:
        if direction.x < 0:
           velocity.y+= delta * 100
        elif 0 < direction.x:
           velocity.y-= delta * 100

func _physics_process(delta):
    handlInput(delta)
    move_and_slide()

something like this, you just have to connect the body entered and exited signals to set on_stairs to true or false

1 Like

still nothing… the character goes in the area2d but nothing happens. I have connceted the body_entered signal to the player script and made the on_stairs True there but nothing.

extends CharacterBody2D
@export var speed: int = 100
var on_stairs = false
var direction=Vector2.ZERO
func handlInput(delta):
	direction=Input.get_vector("ui_left","ui_right","ui_up","ui_down")
	velocity = direction*speed
	if on_stairs==true:
		if direction.x < 0:
			velocity.y+=delta * 100
		elif direction.x > 0:
			velocity.y-= delta * 100


func _physics_process(delta):
	handlInput(delta)
	move_and_slide()
	

func _on_peleh_body_entered(body):
	on_stairs=true

fyi the _on_peleh_body_entered is the area2d signal

actually with velocity you don’t need delta anyway…

can you change it to position (how you originally did it) and see if it works:

if on_stairs==true:
		if direction.x < 0:
			position.y+=delta * 100
		elif direction.x > 0:
			position.y-= delta * 100

thank you

1 Like

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