Movement tutorial, velocity cap


Hello, i just started working with LUA and Defold, and have been doing the tutorials. When i finished the movement tutorial, there were some extra exercises i wanted to complete. I want to make a movementspeed cap for my object, but i cant seem to solve it.

local acceleration = self.input * 200

local dv = acceleration * dt
local v0 = self.velocity
local v1 = self.velocity + dv
local movement = (v0 + v1) * dt * 0.5

local p = go.get_position()
go.set_position(p + movement)

self.velocity = v1
self.input = vmath.vector3()

I wonder if there is a way to assign a max and min value for my vector v1, which will make the movement varible have a max, and min value? Thanks for help!


Hi and welcome!
It sounds that it should be possible to use the built-in functions math.min and math.max around movement. :slight_smile: