Jump to content

Calculate distance between inst and target


Recommended Posts

I was trying to make damage scaled to distance between target and attacker - make longer distance between them should deal more damage, but I can't find how to get distance.

inst._DoAttack = inst.components.combat.DoAttack
	inst.components.combat.DoAttack = function(self, targ, weapon, projectile, stimuli, instancemult)
		if weapon == nil then
			weapon = inst.components.combat:GetWeapon()
		end
		if weapon.components.weapon.attackrange and weapon.components.weapon.attackrange >= 4 then
			print("ya hav som range")
			instancemult = instancemult and instancemult + (weapon.components.weapon.attackrange - 4)/10 or 1 + (weapon.components.weapon.attackrange - 4)/10
		end
		inst._DoAttack(self, targ, weapon, projectile, stimuli, instancemult)
	end

this was first prototype one but this only used weapon's attack range, which is totally not my intention.

so I used Pythagoras' theorem to get distance like:

inst._DoAttack = inst.components.combat.DoAttack
	inst.components.combat.DoAttack = function(self, targ, weapon, projectile, stimuli, instancemult)
		if targ == nil then
			targ = inst
		end

		local atkx, atky, atkz = inst.Transform:GetWorldPosition()
		local tarx, tary, tarz = targ.Transform:GetWorldPosition()
  
		local distance = ((atkx - tarx)^2 + (atky - tary)^2 + (atkz - tarz)^2)^(1/2)
		--in most case y is zero, only flying thing could have y axis

		if distance then
			instancemult = instancemult and instancemult * (distance^(1/2)/10) or 1 + distance^(1/2)/10
		end
		inst._DoAttack(self, targ, weapon, projectile, stimuli, instancemult)
	end

it works!

It should give near 131% damage bonus when attack distance is 10(full range of blowdart).

only distance-getting-code is here if you need in case:

local function MeasureDistance(inst, targ)
	local tarx, tary, tarz = 0, 0, 0

	if targ then
		tarx, tary, tarz = targ.Transform:GetWorldPosition()
	else
		tarx, tary, tarz = inst.Transform:GetWorldPosition()
	end

	local stdx, stdy, stdz = inst.Transform:GetWorldPosition()
	local distance = ((stdx - tarx)^2 + (stdy - tary)^2 + (stdz - tarz)^2)^(1/2)
	--in most case y is zero, only flying thing could have y axis
	return distance
end

well at first I was asking how to get distance between inst and target but I somehow made it so I post it to help someone like me

 

edit: actually behaviours/follow.lua already has distance-measuring function, I should look more in codes. thanks for watching newb modder's code

Edited by Combustiblemon
already follow has dist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...