#How to imitate jump by using sprite, not KinematicBody?

18 messages · Page 1 of 1 (latest)

torn lance
#

When 'ui_accept' is pressed, it just teleport the sprite to coordinates. How to code it so it will imitate the simple jump?

nova kraken
#

the math is the same

#

instead of setting offset directly have a velocity vector

#

add velocity * delta to offset

torn lance
#

do I need a move_and_slide method? its not working with a sprite

nova kraken
#

offset += velocity * delta

#

you don't need a method

#

although why not just use a kinematicbody2d?

torn lance
#

I'm trying to imitate Z axis (2.5d game). Moving offset and collision seems like the simplest way to achieve my goal

#

extends Sprite

const GRAVITY = 1
const JUMPFORSE = -100

var velocity = Vector2()

func _process(delta):
offset.y = offset.y + GRAVITY
if offset.y > 0:
offset.y = 0
if Input.is_action_pressed("ui_accept"):
offset += velocity * delta

#

nothing happen when I click ui_accept

nova kraken
#

you want to add Gravity * delta instead of just gravity each frame

#

so it's not frame dependent

#

you also want to make sure that the offset doesn't go past a number

#

so

#
func _process(delta):
  velocity.y += GRAVITY * delta
  if Input.is_action_pressed("ui_accept"):
    velocity.y = JUMPFORSE
  offset += velocity * delta
olive granite
#

Have you tried printing offset to ensure the value is changing as expected?

torn lance