Godot Version
4.4.1
Question
I tried to make a character switch scenes when walking into an area but now it just flys off screen instaly when i run it. Do anyone see a problem or do i need to send upload more? When i remove the move_slide function my character does nothing but with it it flys away to the left
Somehow, your velocity is set very high. You may have to post more code, what is speed set to?
Make sure to paste code instead of screenshots.
sorry i reply a little late. This is my whole player code:
extends CharacterBody2D
const speed = 100
const TILE_SIZE = 16
var current_dir = “none”
func _ready() → void:
$AnimatedSprite2D.play(“Idle”)
func _physics_process(delta: float) → void:
player_movement(delta)
func player_movement(delta):
if Input.is_action_pressed("ui_right"):
current_dir = "right"
play_anim(1)
velocity.x = speed
velocity.y = 0
elif Input.is_action_pressed("ui_left"):
current_dir = "left"
play_anim(1)
velocity.x = -speed
velocity.y = 0
elif Input.is_action_pressed("ui_down"):
current_dir = "down"
play_anim(1)
velocity.y = speed
velocity.x = 0
elif Input.is_action_pressed("ui_up"):
current_dir = "up"
play_anim(1)
velocity.y = -speed
velocity.x = 0
else:
play_anim(0)
velocity.x = 0
velocity.y = 0
move_and_slide()
func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D
if dir == "right":
anim.flip_h = true
if movement == 1:
anim.play("Walk")
elif movement == 0:
anim.play("Idle")
if dir == "left":
anim.flip_h = false
if movement == 1:
anim.play("Walk")
elif movement == 0:
anim.play("Idle")
if dir == "down":
anim.flip_h = false
if movement == 1:
anim.play("Walk")
elif movement == 0:
anim.play("Idle")
if dir == "up":
if movement == 1:
anim.play("Walk")
elif movement == 0:
anim.play("Idle")
It worked a week ago but when i created a new script for the main it didn’t even run and when i deleted the new script it the issuse started. Don’t even know if this code is whats wrong
I think in your movement function you could use the position instead of velocity.
Or use var dir := Input.get_vector(“Action 1”, “Action 2”, “Action 3”, “Action 4”)
velocity = SPEED * dir
move_and_slide
Maybe this would work, If I find a better solution, I will say so.
1 Like