Performing an operation when two objects are within a certain distance

I’m trying detect when two objects get with a certain and perform an operation when that happens using an ‘if’ statement. Unfortunately I’m getting the error ‘attempt to compare userdata with number’. My code is below.

local GRAVITY = 1000
function init(self)
	self.velocity = vmath.vector3()
end

function update(self, dt)
	local current_position = go.get_position()
	local imeda_position = go.get_position('imeda')
	if imeda_position - current_position <= 1000 then
		print ('imeda!')
		local direction = vmath.normalize(go.get_position('imeda') - current_position)
		self.velocity = self.velocity + (direction * GRAVITY * dt)
		go.set_position(current_position + (self.velocity * dt))
	end
end

Any help would be much appreciated, sorry if this seems like a stupid question or something.

This results in another vector.
You’ll want to calculate the length of that vector.

local diff = imeda_position - current_position
local distance = vmath.length(diff)
3 Likes