I am using a slightly altered homing missile script from a couple builds back, then used by the coalition missile launcher.
It mostly does the job, except that it sees enemies through terrain and thus missiles often smack right into the ground because someone's hiding down below.
I am wondering if this script could be altered somehow (with morays? ), so missiles avoid going after an enemy hiding behind terrain?
This is the same line of code I gave to 4zk not long ago, I still don't know if it works.
Code:
function Create(self) --The timer that will measure out the missile's events. self.LTimer = Timer();
--The delay timer at the start self.DelayTimer = Timer(); self.DelayInterval = 500; self.DelayTimer:Reset();
--The missile's target, currently set to nothing.
self.target = nil;
--Find out who shot the weapon by finding the closest actor within 50 pixels. local curdist = 50; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist and actor.ClassName ~= "ADoor" then if SceneMan:CastFindMORay(self.Pos, actor.Pos, actor.RootID, Vector(), 0, false, 1) = true then curdist = dist; self.parent = actor; end end end
--If the missile has no firer, make it go after anyone. Otherwise, set its team to that of the firer. if MovableMan:IsActor(self.parent) then self.Team = self.parent.Team; else self.Team = -1; end end
function Update(self) if self.DelayTimer:IsPastSimMS(self.DelayInterval) then if self.LTimer:IsPastSimMS(250) or MovableMan:IsActor(self.target) == false then --Get a target. Go for the closest actor within 500 pixels. if MovableMan:IsActor(self.target) == false then local curdist = 500; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist and actor.Team ~= self.Team and actor.ClassName ~= "ADoor" then if SceneMan:CastFindMORay(self.Pos, actor.Pos, actor.RootID, Vector(), 0, false, 1) = true then curdist = dist; self.target = actor; end end end end
--If the target still exists... if MovableMan:IsActor(self.target) then
--Make sure the missile's thruster is firing. if self:IsEmitting() == false then self:EnableEmission(true); end
local turnspeed = 0.5; local maxspeed = 30;
local targetdir = math.atan2(-(self.target.Pos.Y-self.Pos.Y),(self.target.Pos.X-self.Pos.X)); local desiredspeedx = math.cos(targetdir) * maxspeed local desiredspeedy = math.sin(targetdir) * (-maxspeed)
if self.Vel.X < desiredspeedx then self.Vel.X = self.Vel.X + turnspeed elseif self.Vel.X > desiredspeedx then self.Vel.X = self.Vel.X - turnspeed end if self.Vel.Y < desiredspeedy then self.Vel.Y = self.Vel.Y + turnspeed elseif self.Vel.Y > desiredspeedy then self.Vel.Y = self.Vel.Y - turnspeed end
else --If there's no target, shut off the thrusters. if self:IsEmitting() == true then self:EnableEmission(false); end end
if self.LTimer:IsPastSimMS(5000) then --If the missile has run out of time, self destruct! self:GibThis(); end end else --Make sure the missile's thruster is firing. if self:IsEmitting() == false then self:EnableEmission(true); end end end
function Create(self) --The timer that will measure out the missile's events. self.LTimer = Timer();
--The delay timer at the start self.DelayTimer = Timer(); self.DelayInterval = 500; self.DelayTimer:Reset();
--The missile's target, currently set to nothing.
self.target = nil;
--Find out who shot the weapon by finding the closest actor within 50 pixels. local curdist = 50; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist and actor.ClassName ~= "ADoor" then if SceneMan:CastFindMORay(self.Pos, actor.Pos, actor.RootID, Vector(), 0, false, 1) == true then curdist = dist; self.parent = actor; end end end
--If the missile has no firer, make it go after anyone. Otherwise, set its team to that of the firer. if MovableMan:IsActor(self.parent) then self.Team = self.parent.Team; else self.Team = -1; end end
function Update(self) if self.DelayTimer:IsPastSimMS(self.DelayInterval) then if self.LTimer:IsPastSimMS(250) or MovableMan:IsActor(self.target) == false then --Get a target. Go for the closest actor within 500 pixels. if MovableMan:IsActor(self.target) == false then local curdist = 500; for actor in MovableMan.Actors do local avgx = actor.Pos.X - self.Pos.X; local avgy = actor.Pos.Y - self.Pos.Y; local dist = math.sqrt(avgx ^ 2 + avgy ^ 2); if dist < curdist and actor.Team ~= self.Team and actor.ClassName ~= "ADoor" then if SceneMan:CastFindMORay(self.Pos, actor.Pos, actor.RootID, Vector(), 0, false, 1) == true then curdist = dist; self.target = actor; end end end end
--If the target still exists... if MovableMan:IsActor(self.target) then
--Make sure the missile's thruster is firing. if self:IsEmitting() == false then self:EnableEmission(true); end
local turnspeed = 0.5; local maxspeed = 30;
local targetdir = math.atan2(-(self.target.Pos.Y-self.Pos.Y),(self.target.Pos.X-self.Pos.X)); local desiredspeedx = math.cos(targetdir) * maxspeed local desiredspeedy = math.sin(targetdir) * (-maxspeed)
if self.Vel.X < desiredspeedx then self.Vel.X = self.Vel.X + turnspeed elseif self.Vel.X > desiredspeedx then self.Vel.X = self.Vel.X - turnspeed end if self.Vel.Y < desiredspeedy then self.Vel.Y = self.Vel.Y + turnspeed elseif self.Vel.Y > desiredspeedy then self.Vel.Y = self.Vel.Y - turnspeed end
else --If there's no target, shut off the thrusters. if self:IsEmitting() == true then self:EnableEmission(false); end end
if self.LTimer:IsPastSimMS(5000) then --If the missile has run out of time, self destruct! self:GibThis(); end end else --Make sure the missile's thruster is firing. if self:IsEmitting() == false then self:EnableEmission(true); end end end
Thanks! This works like a charm, almost... Gawd, with new functionality comes new problems.
The missile doesn't check for a new target. Once it fires and there's nothing at that particular moment in its line of sight it'll just go straight forward, even when a target might come up during its flight. It makes it impossible, for example, to fire over a hilltop and have the missile go after something behind the hill.
Attachment:
missile.png [ 2.7 KiB | Viewed 7463 times ]
Would this be possible? I'd guess some sort of continuous check would create lag the size of New York, so maybe it's possible to do such a check every half second or so?
Thanks for your help so far. I really appreciate it.
Tue Jul 10, 2012 12:50 am
CaveCricket48
Joined: Tue Jun 12, 2007 11:52 pm Posts: 13144 Location: Here
Thanks, CaveCricket. They both have the missile aim at enemies, but the missile still doesn't go towards them. It keeps going in a straight line. The missile 'looks' at its target and tracks it as it just flies by.
Attachment:
missile2.png [ 2.89 KiB | Viewed 7434 times ]
I guess it must be related to my missile, although it has a pushing emitter. I'll try comparing it with yours again.
Last edited by Gotcha! on Tue Jul 10, 2012 11:58 am, edited 1 time in total.
Tue Jul 10, 2012 11:54 am
4zK
Joined: Mon Oct 11, 2010 1:15 pm Posts: 594 Location: Finlandia
They both have the missile aim at enemies, but the missile still doesn't go towards them. It keeps going in a straight line. The missile 'looks' at its target and tracks it as it just flies by.
Ironically, this is exactly what I was looking for.
Well, if your 'missile' was in fact some kind of gun drone... launch it over a mob/obstacle/whatever and have it scripted to spit the bullet particles at any enemies it detects. That'd be pretty useful, no?
@CaveCricket48: Sure! But, err, believe it or not, I was comparing your missile with mine 30 minutes ago, modifying stuff that shouldn't matter according to my logic, and I found the problem. Seems OrientToVel = 1 is a bad property to use in conjunction with those scripts. When that is there they just stop working.
Now all I need to do is balance the amount of emitted particles to have the right flight speed.
Thanks, man!
Edit: Oh, and your experimental script is bloody amazing, man! O_O
@Arcalane: Hmm, yes, I guess so. My fantasy is way too limited.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum