So i need help with movement of character in 2d and changing sprites why he's moving

Godot Version

4.5

Question

I have a problem with movement of character. He didn't move at right (at all) and his sprite stay at left all the time OR change only one time. pls someone save me..

this is my code:

extends CharacterBody2D
var max_speed = 500

var right = preload("res://characters/character_sprites/right.png")
var left = preload("res://characters/character_sprites/left.png")
var up = preload("res://characters/character_sprites/back.png")

@onready var sprite_right = $right
@onready var sprite_left = $left
@onready var sprite_up = $up

func _ready():
	pass

func _physics_process(_delta):
	var movement = movement_vector()
	var directions = movement.normalized()
	velocity = directions * max_speed
	move_and_slide()
	handle_rotation()

func movement_vector():
	var movement_x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	var movement_y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	return Vector2(movement_x, movement_y)

func handle_rotation():
	var input = Vector2.ZERO
	input.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	input.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	if input.x > 0:
		$right.visible = true
		$left.visible = false
		$up.visible = false
	elif input.x < 0:
		$right.visible = false
		$left.visible = true
		$up.visible = false

Hey!
Can you please put the code inside the Preformatted text ,it makes easier to read.
You can toggle that by pressing Ctrl + E

I think there’s a problem with your sprites,I have tried this simplified version and seems like it works!

	if input.x > 0:
		#$right.visible = true
		print("right")
		#$left.visible = false
		#$up.visible = false
	elif input.x < 0:
		print("left")
		#$right.visible = false
		#$left.visible = true
		#$up.visible = false

maybe you didn’t added the sprites for the right side.It’s better to flip the sprite if both left and right sprites are the same.You can do it like this

	if input.x > 0:
		$BaseSprite.flip_h = true # true or false ,depending on what direction your base sprite is looking at
		print("right")
		
	elif input.x < 0:
		print("left")
		$BaseSprite.flip_h = false

You can also use an AnimatedSprite2D for the best results!

thank you, now I finally know how to write code here haha

I tried change sprites here, but it didn’t worn and, well, why player don’t move right anyway? I don’t think it depens on sprites..

Is the problem about the player not moving?
Can you clarify a bit : )

Yes, the player isn’t moving correctly. Or more precisely, he’s only moving to the left.

hmm,that’s very odd ,it works on mine with your script.Can you check the input maps in the project settings? That also can be an overlapping or glichy collusion thing.can you show the player collusion ?

well it also sometimes give me this error
Invalid assignment of property or key ‘visible’ with value of type ‘bool’ on a base object of type ‘null instance’.

that should not happen.
Can you take a screen shot of that error? seems like there is some messy code going on xD
and can you clarify that can you move your player in both directions? and what is the problem exactly?

well it looks like that

now, when I try to move this bug happens, so no, I can’t move at all..

Wheres the up sprite…???
lol,you only got the back sprite

the silly bugs that seems complex but it doesn’t even exist xD

oh.
Someday my carelessness will kill me.

HE’S MOVING

but the bug with moving only to the right still exists

Nahh,That’s just a game dev thing

have a nice day!

hmm,Just check these again,I am pretty sure that’s a silly mistake again,you should try flipping the sprites instead of making em visible and invisible.
use flip_h as I mentioned in my first post

What does your Project > Project Settings > Input Map look like

1 Like

ohhhh,
you using wasd to use control your player?

1 Like

it would be cool to just flip them but sprites are different here..
I have a feeling that the mistake is so stupid that I will never find it.