It gives me this error in the debugger
The character does not move down and up.
When I get a chance I will open the zip and see what the heck is going on. :)
Ok. Thank you.
(Note: I see 14 replies were posted before I've hit Post on this, so I may be doubling up a little. Sorry, just ignore if someone beat me to bits of it)
You were printing out the joystick x values (line 20 above), what kind of numbers did you get? Minimum and maximum. The code looks like it's written for a 0 to 1 range with 0.5 (umbralJoystick) in the middle. That's because it's saying x < 0.5 is left and x > 0.5 is right. But also 0 is idle, which wouldn't fit that. Are the values being printed in the range -1 to 1 (with 0 in the middle)? Or bigger? Looking at the joystick code, it's using a touch screen which I've never used in Godot, so not sure what that part is doing, but it looks like it's returning a position added to (24,24) then normalised, so that's probably returning positive numbers only.
Assuming it's -1 to 1 (the way a joystick would normally be used), the joystick code would need to be like this:
func getControlJoystick():
if joystick.get_value().x == 0 && joystick.get_value().y == 0:
idle()
elif joystick.get_value().x > umbralJoystick:
correrDerecha()
elif joystick.get_value().x < -umbralJoystick:
correrIzquierda()
elif joystick.get_value().y > umbralJoystick:
correrAbajo()
elif joystick.get_value().y < -umbralJoystick:
correrArriba()
This is only allowing 4 directions (like pacman) though, did you want to be able to move on diagonals? Also a joystick value that's in the deadzone (below umbralJoystick) but not zero isn't idle.
It may be better to disable the joystick check and put the keyboard check back in. Keyboard debugging is going to be easier than touch screen. Then come back to joystick once it's working.
Next, both of your up and down functions are adding to y. Adding moves down. You need the up function (correrArriba) to subtract RUN_SPEED instead.
Anyway, here's a complete working keyboard based version. I removed the animation state machine, I didn't want to have to set that up just to test. :) But up, down, left and right work with physics collisions.
extends KinematicBody2D
var player: Vector2
var RUN_SPEED = 200
const umbralJoystick = 0.5
func _ready():
pass
# warning-ignore:unused_argument
func _physics_process(delta):
player = Vector2()
getControlsKeyboard()
# warning-ignore:return_value_discarded
move_and_slide(player)
#Controlamos al personaje con el teclado
func getControlsKeyboard():
if Input.is_action_pressed("ui_right"):
correrDerecha()
elif Input.is_action_pressed("ui_left"):
correrIzquierda()
if Input.is_action_pressed("ui_down"):
correrAbajo()
elif Input.is_action_pressed("ui_up"):
correrArriba()
func idle():
pass
func correrDerecha():
player.x += RUN_SPEED
$boy.scale.x = 1
func correrIzquierda():
player.x -= RUN_SPEED
$boy.scale.x = -1
func correrAbajo():
player.y += RUN_SPEED
$boy.scale.y = 1
func correrArriba():
player.y -= RUN_SPEED
$boy.scale.y = -1
Depending on how you've set up your input mapping, you might need to change the key names. ui_left, ui_right, ui_up and ui_down are the defaults that Godot sets up for you.
Once that's working, we can come back to the joystick control.
- Edited
comment out this line:
# print("joystick" + str(joystick.get_value().x))
then try the up and down again. Did you change:
if Input.is_action_pressed("ui_up"):
It should print.
- Edited
It only moves down, left and right.
Put the print in the up function and see if it prints
@fire7side said: Put the print in the up function and see if it prints
Where I put?
Like last time:
func correrArriba():
player.y -= RUN_SPEED
$boy.scale.y = -1
print("up")
whichever is up.
Now if you let me move to all sides.
How can I make the player to be removed when he touches the mob?
- Edited
What mob? Are you doing a tutorial or something? The print statement isn't supposed to stay there.
I following the dodge_the_creeps but with kinematicbody2d to use the joystick.
How to detect the mob largely depends on what kind of physics body it is. Attach a signal from the mob to the player.
- Edited
How can I attach a signal from the mob to the player?
Is the mob also a kinematic body 2d?
No. It is a RigidBody2D
Add an Area2D with a collision shape to it. That will serve the signal for the kinematic2D.
Where I added?
What I would personally do is open the mob scene and add the area2D. Then connect the signal from the area to the mob.
The mob, in turn, will send a custom signal to the player, telling it that it was hit.