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
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!
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?
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