![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | verbaloid |
I’m designing an oldschool Spectrum-like platformer where I need several jump tweaks I can’t quite figure out.
Two problems with the player code below:
1. The player jumps only with
if Input.is_action_pressed("player_jump"):
Problem with the jump sound is that it retriggers every frame, whereas I need it only to play once. Which happens if I set it to
if Input.is_action_just_pressed("player_jump"):
- but then the player will jump only very low. How can I fix this? Should I use _just_pressed?
2. I need to set the maximum jump height after which the velocity.y will lerp to gravity pull. There will be tiles in the game that let the player jump higher (temporarily. How should I do that better?
Here’s the code:
extends KinematicBody2D
var airborne
var movingright
var velocity = Vector2()
var walkspeed = 400
var gravityscale = 600
var jumpvelocity = 1300
func _ready():
movingright = true
func teleport_to(target_pos):
position = target_pos
func get_input():
if Input.is_action_pressed("ui_right"):
movingright = true
airborne = false
$SnoozyAnim.play("walk")
velocity.x = walkspeed
elif Input.is_action_pressed("ui_left"):
movingright = false
airborne = false
$SnoozyAnim.play("walk")
velocity.x = - walkspeed
else:
$SnoozyAnim.play("idle")
airborne = false
velocity.x = lerp(velocity.x, 0, 0.2)
if not movingright:
$SnoozyAnim.set_flip_h(false)
else:
$SnoozyAnim.set_flip_h(true)
if Input.is_action_pressed("player_jump"):
velocity.y -= jumpvelocity
airborne = true
$SnoozyAnim.play("jump")
$JumpSound.play()
func _physics_process(delta):
velocity.y = gravityscale
get_input()
move_and_slide(velocity)
I think its better to do one thing until you are confortable with the result and then start the next stuff. Try to do the character movement works well, then add the animations, e after that the sound. It becomes easier when you break all the game in small goals.
JVStorck | 2020-01-10 18:59