Forum

> > CS2D > Scripts > Projectile targets enemy
Forums overviewCS2D overview Scripts overviewLog in to reply

English Projectile targets enemy

2 replies
To the start Previous 1 Next To the start

old Projectile targets enemy

Ace Howl
User Off Offline

Quote
Hello.

1
2
3
4
5
6
7
addhook("serveraction","_sa")

function _sa(id)
	if player(id,"money")> 999 and act==3 then -- requires money to activate
		parse("spawnprojectile 47 "..player(id,"x").." "..player(id,"y").." "..z.." "..a)
	end
end

What I need: projectile that travel towards nearest enemy (range setting would be nice)

As title explains, I don't know how to replace z and a with something that makes it to launch towards them.

Assuming that:
z = nearest enemy distance
a= nearest enemy position

Not meaning that it chases enemy until end of the world, the projectile only lands on the last their position at the moment the launcher being released.

PS: the money activation is just example, though.

old Re: Projectile targets enemy

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
-- finding nearest player
local lowest = 0, targetx, targety, distance, angle
for k,v in pairs (player(0,"tableliving")) do
	if player(v,"team") ~= player(id,"team") then
		distance = math.sqrt((player(v,"x")-player(id,"x"))^2 + (player(v,"y")-player(id,"y"))^2)
		if distance < lowest or lowest == 0 then
			lowest = distance
			targetx = player(v,"x")
			targety = player(v,"y")
		end
	end
end
parse("spawnprojectile 47 "..player(id,"x").." "..player(id,"y").." "..lowest.." "..angle)
as for angle, I'd advise to use
1
2
3
math.atan2
targetx
targety
then convert it into degrees. I'm afraid I'm too lazy to do those calculations and I haven't used them for a long time so it would be too much of a bother to look them up. I'll just hope someone else calculates that for you.
Alternatively, you could learn a bit of trygonometry and read up about math functions in lua here
Good luck.

old Re: Projectile targets enemy

Ace Howl
User Off Offline

Quote
user Rainoth has written
1
2
3
4
5
6
7
8
9
10
11
12
13
-- finding nearest player
local lowest = 0, targetx, targety, distance, angle
for k,v in pairs (player(0,"tableliving")) do
	if player(v,"team") ~= player(id,"team") then
		distance = math.sqrt((player(v,"x")-player(id,"x"))^2 + (player(v,"y")-player(id,"y"))^2)
		if distance < lowest or lowest == 0 then
			lowest = distance
			targetx = player(v,"x")
			targety = player(v,"y")
		end
	end
end
parse("spawnprojectile 47 "..player(id,"x").." "..player(id,"y").." "..lowest.." "..angle)


Oh ya, I forgot the distance should be square rooted. BTW, my goodness, I don't think that I can solve angle var in trigonometry in LUA version (just learned trigonometry Lv1 student here (trigonometry in real and code would be barely different, i think)). However, I will try other solution in substitution of the calculations. Thanks for your effort, user Rainoth √
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview