Hello everyone, For my first game using the godot engine i'm trying to reproduce the game 'Jump King'. This game is realy simple, you move left and right, you load your jump and cannot change its direction while not on ground. I've already implements the controls but my jump look really awkward because of my character moving first on the x axis and then on the y one. I wonder if there's a way to make my jump more 'realistic' by modifying my Vector2D.x and Vector2D.y at the same time or by using some functions. Thank you in advance for your replies.
Jump King like jumps
Welcome to the forums @DarkLeksis!
What does your code look like? Generally for jumping smoothly in multiple directions (up and forward, etc) you want to modify the velocity of the character and then use something like KinematicBody or KinematicBody2D's move_and_slide
function to move the character based on the velocity passed.
The Godot demo repository has some examples of 2D and 3D characters that may be helpful as a code reference.
- Edited
- I've set velocity as a Vector2D
- 'end_jmp' is the bool that verifie the released of the jump button.
- 'cumul_jmp' and 'cumul _dir' are int that represent speed acquired
if end_jmp: #end_jmp confirme la vel.x et vel
velocity.y = cumul_jmp
velocity.x += cumul_dir*15
this part is in a function called just before my move_and_slide() method:
velocity = move_and_slide(velocity, Vector2.UP)
@DarkLeksis said: 1. I've set velocity as a Vector2D 2. 'end_jmp' is the bool that verifie the released of the jump button. 3. 'cumul_jmp' and 'cumul _dir' are int that represent speed acquired
if end_jmp: #end_jmp confirme la vel.x et vel velocity.y = cumul_jmp velocity.x += cumul_dir*15
this part is in a function called just before my move_and_slide() method:
velocity = move_and_slide(velocity, Vector2.UP)
Hmm, maybe try setting the X velocity once (instead of +=
) and see if that fixes it? It could be that by adding the velocity, it's making it where the X velocity is significantly larger than the Y and therefore there is more movement on the X axis than the Y initially.
That said though, I don't see anything right off in the code snippet that would cause an issue per say, so I'm not totally sure. One thing you could try is adding print statement to check what the velocity is after pressing and/or while jumping to see if something is happening with the velocity that is causing the issue.
- Edited
i've done what you said and adjusted the speed, but the jump still seems 'unrealistic'. I've the impression that my x velocity is canceled by the y velocity. My character is perfectly fluid on the y, but dont move that much on the x (for exemple with a 1200 speed in the air it teleport for ~64px to the left/right) and tp in one frame. Here's my code for more precision: ( because of my weak english)
func move_manager():
velocity.x = 0
#chargement du saut
var jmp = Input.is_action_pressed("up")
var end_jmp = Input.is_action_just_released("up")
# direction du saut (= saut + touche de deplacement)
var jmp_right = Input.is_action_pressed("right") and jmp
var jmp_left = Input.is_action_pressed("left") and jmp
# (deplacement sur le sol)
var right = Input.is_action_pressed("right") and is_on_floor() and not jmp
var left = Input.is_action_pressed("left") and is_on_floor() and not jmp
var dir_dep = int(right) - int(left)
# deplacement gauche droite sur le sol
velocity.x += (SPEED/4)*dir_dep
# charge saut gauche et droite
if jmp and is_on_floor():
if cumul_jmp > JMP_MAX_HEIGHT:
cumul_jmp -= 15
if jmp_right:
if cumul_dir < SPEED:
cumul_dir += 40
if jmp_left:
if cumul_dir > -SPEED:
cumul_dir -= 40
if end_jmp: #end_jmp confirme la vel.x et vel
velocity.x = cumul_dir
velocity.y = cumul_jmp
# reset
cumul_jmp = 0
cumul_dir = 0
Try checking if the player is in the air on line 2, as you are setting it to zero even if the player is in the air, which is probably what is leading it not to move very much on the X but it works smoothly on the Y, since after the first frame of the jump the horizontal velocity is reset to 0.
- Edited
@TwistedTwigleg said: Try checking if the player is in the air on line 2, as you are setting it to zero even if the player is in the air, which is probably what is leading it not to move very much on the X but it works smoothly on the Y, since after the first frame of the jump the horizontal velocity is reset to 0.
Thank you so much, i totaly forgot that i canceled momentum on ground.