
I am very new to lua and i just want to know how i can get an object to move a designated length
instead of teleporting instantly

For example when jumping.
I just want to tap w for example and i want the player to move up a specific length smoothly.
Code: Select all
x-position = 400
Code: Select all
x-position = x-position + speed
Code: Select all
function love.main()
image = newImage("image")
x = 10
y = 200
speed = 100
end
function love.update(dt)
x = x + speed * dt
end
function love.draw()
love.graphics.draw(image,x,y)
end
Sheepolution wrote:So you can do this:
That's teleporting.Code: Select all
x-position = 400
But what you want to do is this:
And I can recommend you to use dt (deltatime).Code: Select all
x-position = x-position + speed
So in the end you get something like this:
Code: Select all
function love.main() image = newImage("image") x = 10 y = 200 speed = 100 end function love.update(dt) x = x + speed * dt end function love.draw() love.graphics.draw(image,x,y) end
Is it for jumping or for smooth movement?WahWah wrote:Yeah. I already know that
What i mean is that i want to move a desired amount of pixels and then stop.
Thanks anyway
Code: Select all
velocity.y = velocity.y + gravity * dt
Oh... I get it nowmicha wrote:Is it for jumping or for smooth movement?WahWah wrote:Yeah. I already know that
What i mean is that i want to move a desired amount of pixels and then stop.
Thanks anyway
If you want to implement jumping, then you do it just like Sheepolution suggested and in each timestep doThat way the object will have a highest point and fall back down.Code: Select all
velocity.y = velocity.y + gravity * dt
If you just want smooth movement you need two pairs of coordinates: target-coordinates and actual-coordinates. The actual-coordinates are the ones, where you draw the object and the target-coordinates are the ones, where the object should move. Each timestep move the actual-coordinates towards the target-coordinates (it's called tweening).
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], pgimeno and 3 guests