Forum

> > CS2D > Scripts > Projectile targets enemy
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Projectile targets enemy

2 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Projectile targets enemy

Ace Howl
User Off Offline

Zitieren
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.

alt Re: Projectile targets enemy

Rainoth
Moderator Off Offline

Zitieren
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.

alt Re: Projectile targets enemy

Ace Howl
User Off Offline

Zitieren
user Rainoth hat geschrieben
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 √
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht