Hello!
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.
Moving....stuff!
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Sheepolution
- Party member
- Posts: 264
- Joined: Mon Mar 04, 2013 9:31 am
- Location: The Netherlands
- Contact:
Re: Moving....stuff!
So you can do this:
That's teleporting.
But what you want to do is this:
And I can recommend you to use dt (deltatime).
So in the end you get something like this:
Code: Select all
x-position = 400
But what you want to do is this:
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
Re: Moving....stuff!
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
Yeah. I already know that
What i mean is that i want to move a desired amount of pixels and then stop.
Thanks anyway
Re: Moving....stuff!
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 do
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).
Check out my blog on gamedev
Re: Moving....stuff!
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).
Thanks
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests