Smooth movement to a fixed position (SOLVED)

Hi there!

How do I make my player’s movement more smoother?

If pressed the up button — game object should move by 128 pixels on the y-coordinate. If pressed the down button — game object should move by -128 pixels on the y-coordinate. So my game object has 4 movements: from center to top, from top to center, from center to bottom and from bottom to center.

function init(self)
	msg.post(".", "acquire_input_focus")

	self.movement = false
	
	self.targetPos = vmath.vector3()
	
	self.maxYCoord = 696
	self.minYCoord = 440
end

function update(self, dt)
	if self.movement then
		local pos = go.get_position()
		pos = pos + self.targetPos
		go.set_position(pos)
	end

	self.targetPos.y = 0
	self.movement = false
end

function on_input(self, action_id, action)
	if action_id == hash("up") and action.pressed and go.get_position().y < self.maxYCoord then
		self.targetPos.y = 128
		self.movement = true
	elseif action_id == hash("down") and action.pressed and go.get_position().y > self.minYCoord then
		self.targetPos.y = -128
		self.movement = true
	end
end

P. S. Sorry for my English.

You can animate properties too, read this: https://www.defold.com/examples/animation/tween/

2 Likes

Yes, but something tells me that it’s not the best solution for me.

UPD. I tried to use “go.animation” and it works. Can anyone tell me — is it fine to use go.animation to control player’s movement?

Yes, it’s fine when you move from one given point to another.

1 Like

Thank you!

One common way to get smooth motion, is to use the delta time variable (dt).
With it you know how much time has lapsed between the last frame and the current.

Here’s a simple example of how to do it.

function init(self)
	msg.post(".","acquire_input_focus")
	self.speed = 50 -- pixels per second
	self.direction = vmath.vector3(0,0,0)
end

function update(self, dt)
	local position = go.get_position() + (dt * self.speed) * self.direction
	go.set_position(position)
	self.direction = vmath.vector3(0,0,0)
end

function on_input(self, action_id, action)
	if action_id == hash("left") then
		self.direction.x = self.direction.x - 1
	end
	if action_id == hash("right") then
		self.direction.x = self.direction.x + 1
	end
	if action_id == hash("up") then
		self.direction.y = self.direction.y + 1
	end
	if action_id == hash("down") then
		self.direction.y = self.direction.y - 1
	end
end
4 Likes

The solution from @Mathias_Westerdahl should do the trick for you. In addition, if you want to check another option you can try lerp (https://www.defold.com/ref/vmath/#vmath.lerp:t-v1-v2) as well.

2 Likes

Hi Im trying to do a smooth movement, I have somehting like you post, but I want to add aceleration and I dont know how to implement It.

Anyone can help me pelase?

I am adding in self.direction.x +1 for example without reset betwen frames, but I am normalizing the vector later, then I lost the aceleration, how can I implement aceleration and have the movement normalized? (character going the same speed in diagonal and straight

  • Start with self.speed = 0
  • In update when vmath.length(self.direction) > 0 start increasing speed:
    • self.speed = self.speed + acceleration * dt
  • You need to decide if speed should have an upper limit or not
  • You need to decide if speed should start decreasing when vmath.length(self.direction) == 0
2 Likes

I owe you one :hugs:

1 Like