Jump to content
Kydoimos

Force AI to Fire Launcher

Recommended Posts

Hiya - I know, I know - this thread has been covered a lot - but - I can't seem to find a single solution that works! 'SelectWeapon' doesn't appear to do the trick (unit just grabs at the air). Even when I play an animation to lock the unit into a raised launcher stance, I can't get them to fire! A workaround I guess, would be to spawn a rocket - but not sure how to do that, or even if it's possible? The unitCapture with firing is an option, but not a perfect solution for infantry... any ideas? :)

  • Like 1

Share this post


Link to post
Share on other sites

@PhonicStudios - ah, thanks man! Though I've tried all those! :D Fire and forceWeaponFire I'm sure will work, but I can't seem to get the unit to select the launcher first! I didn't have any luck with UseWeapon either! I do appreciate the help though - thanks!

Share this post


Link to post
Share on other sites

Tried:

u1 selectWeapon (secondaryWeapon u1);

And can confirm, it doesn't work (should, muzzlename is here same as weapon's name). Just nothing happens. Also after taking from the unit primary weapon and handgun. Looks like bug to me. But there is a workaround. Apart from the unit place in his range and LOS a target, that AI usually would engage with launchers (eg a tank). Then execute this:

u1 doFire tank1;

In my test AI then on its own switched to the launcher, aimed and fired.

Share this post


Link to post
Share on other sites
Tried:

u1 selectWeapon (secondaryWeapon u1);

And can confirm, it doesn't work (should, muzzlename is here same as weapon's name). Just nothing happens. Also after taking from the unit primary weapon and handgun. Looks like bug to me. But there is a workaround. Apart from the unit place in his range and LOS a target, that AI usually would engage with launchers (eg a tank). Then execute this:

u1 doFire tank1;

In my test AI then on its own switched to the launcher, aimed and fired.

So, doFire is working for launchers and against armour? That's pretty weird, I thought doFire was buggy in general, hence I always used forceWeaponFire for primary weapons / pistols. Never tried scripted launcher fire though, seemed like too much of a hassle to me. :)

Share this post


Link to post
Share on other sites

Lately I used doFire with pistols, and was kinda working after doWatch/doTarget.

How about spawning a rocket to simulate it being fired? Could that be done?

Although spawned rocket gets some velocity by default, it seems not enough to guide it far, and soon will explode on the ground. So it involves messing with vectors and velocities, especially, you have to also set projectile vector accordingly. Also gravitation have to be counter somehow. I never was good with vectors, sadly, but after some testing I came to such code:

RYD_missile_test = 
{	
_pos1 = _this select 0;
_pos2 = _this select 1;
_class = _this select 2;
_speed = _this select 3;
_random = _this select 4;

_missile = createVehicle [_class, _pos1, [], 0, "NONE"];

while {not (isNull _missile)} do
	{
	_pos1 = position _missile;

	_dX = (_pos2 select 0) - (_pos1 select 0);
	_dY = (_pos2 select 1) - (_pos1 select 1);
	_dZ = (_pos2 select 2) - (_pos1 select 2);

	_vel = velocity _missile;

	_vector = vectorNormalized [_dX,_dY,_dZ];

	_missile setVectorDirAndUp [_vector,[0,_dZ,0]];

	_missile setVelocity [(_vector select 0) * (_speed + (random (2 * _random)) - _random),(_vector select 1) * (_speed + (random (2 * _random)) - _random),(_vector select 2) * (_speed + (random (2 * _random)) - _random/2)];
	sleep 0.1;
	}
};

that may be executed like this:

_pos1 = position u1;//shooter
_pos1 set [2,3];//3 meters over the ground

_pos2 = position v1;//target
_pos2 set [2,1];//1 meter over the ground

_class = "M_Titan_AT";//CfgAmmo class

_speed = 240;

[_pos1,_pos2,_class,_speed,_speed * 0.05] spawn RYD_missile_test;

It's not ready solution though, missile is soundless, perhaps some additional particles would be nice too. And probably can be optimized. Also seems not to be vertically oriented towards target, but always leveled fixed, I think. Not sure, why it's working that way though...

---------- Post added at 17:31 ---------- Previous post was at 16:31 ----------

Also homing version, if you need the missile chase moving target:

RYD_missile_test = 
{	
_pos1 = _this select 0;
_tgt = _this select 1;
_class = _this select 2;
_speed = _this select 3;
_random = _this select 4;

_missile = createVehicle [_class, _pos1, [], 0, "NONE"];

while {not (isNull _missile)} do
	{
	_pos1 = visiblePositionASL _missile;//maybe better?
	_pos2 = eyePos _tgt;

	_dX = (_pos2 select 0) - (_pos1 select 0);
	_dY = (_pos2 select 1) - (_pos1 select 1);
	_dZ = (_pos2 select 2) - (_pos1 select 2);

	_vel = velocity _missile;

	_vector = vectorNormalized [_dX,_dY,_dZ];

	_missile setVectorDirAndUp [_vector,[0,_dZ,0]];

	_missile setVelocity [(_vector select 0) * (_speed + (random (2 * _random)) - _random),(_vector select 1) * (_speed + (random (2 * _random)) - _random),(_vector select 2) * (_speed + (random (2 * _random)) - _random/2)];
	sleep 0.1;
	}
};

v1 is the target vehicle:

_pos1 = position u1;//shooter
_pos1 set [2,2];

_class = "M_Titan_AT";

_speed = 120;//or another of course

[_pos1,v1,_class,_speed,_speed * 0.0125] spawn RYD_missile_test;

For high speed missiles/targets (like AA missile chasing a jet) sleep IMO has to be reduced eg x10. Perhaps same for random factor. Or maybe for jet intercepting speed should be done per frame:

RYD_missile_homing = 
{
	{
	_missile = _x select 0;
	_delAt = [];

	if not (isNull _missile) then
		{
		_tgt = _x select 1;

		if not (isNull _tgt) then
			{
			_speed = _x select 2;
			_random = _x select 3;

			_pos1 = visiblePositionASL _missile;
			_pos2 = eyePos _tgt;

			_dX = (_pos2 select 0) - (_pos1 select 0);
			_dY = (_pos2 select 1) - (_pos1 select 1);
			_dZ = (_pos2 select 2) - (_pos1 select 2);

			_vel = velocity _missile;

			_vector = vectorNormalized [_dX,_dY,_dZ];

			_missile setVectorDirAndUp [_vector,[0,_dZ,0]];

			_missile setVelocity [(_vector select 0) * (_speed + (random (2 * _random)) - _random),(_vector select 1) * (_speed + (random (2 * _random)) - _random),(_vector select 2) * (_speed + (random (2 * _random)) - _random/2)];
			}
		else
			{
			RYD_missiles set [_foreachIndex,0]
			}
		}
	else
		{
		RYD_missiles set [_foreachIndex,0]
		}
	}
foreach RYD_missiles;

RYD_missiles  = RYD_missiles - [0];

if ((count RYD_missiles) < 1) then
	{
	_draw3Deh = missionNamespace getVariable ["RYD_D3DEH",-1];
	removeMissionEventhandler ["Draw3D",_draw3Deh];
	missionNamespace setVariable ["RYD_D3DEH",nil]
	}
};

RYD_missile_spawn = 
{	
_pos1 = _this select 0;
_tgt = _this select 1;
_class = _this select 2;
_speed = _this select 3;
_random = _this select 4;

_missile = createVehicle [_class, _pos1, [], 0, "NONE"];

if (isNil "RYD_missiles") then {RYD_missiles = []};

RYD_missiles pushBack [_missile,_tgt,_speed,_random];

_draw3Deh = missionNamespace getVariable ["RYD_D3DEH",-1];

if (_draw3Deh == -1) then
	{
	_draw3Deh = addMissionEventHandler ["Draw3D",RYD_missile_homing];
	missionNamespace setVariable ["RYD_D3DEH",_draw3Deh]
	};
};

_pos1 = position u1;//shooter
_pos1 set [2,2];

_class = "M_Titan_AA";

_speed = 200;

[_pos1,plane1,_class,_speed,_speed * 0.0125] spawn RYD_missile_spawn;

I feel, random course deviations should be improved though somehow.

Edited by Rydygier
  • Like 1

Share this post


Link to post
Share on other sites

Ah, thanks Rydygier - that's a great script! Cheers for sharing! :)

---------- Post added at 16:50 ---------- Previous post was at 16:50 ----------

@IndeedPete - thanks for the post, mate! Hope you're well!

---------- Post added at 18:15 ---------- Previous post was at 16:50 ----------

Hey guys! Sussed it! Well, sort of - basically, with a combination of playMove, disableAI "ANIM", and forceWeaponFire. Trouble is, you can't make the AI aim very well!

Share this post


Link to post
Share on other sites
playMove, disableAI "ANIM", and forceWeaponFire

:D I can imagine... Nice thinking.

Share this post


Link to post
Share on other sites

Isn't there a way to spawn a locked on titan missile ?

I am trying to build a helicopter training mission where the player has to evade missiles, however, spawning a unit and using doFire just takes too long if the heli is flying at high speeds.

Share this post


Link to post
Share on other sites

For forcing AI fire a rocket at a target, it'd be neat to use hideObject on an invisible (for player) light/armor vehicle,

set the target behind that vehicle, attach a 'fired' EH to AI, move the tank elsewhere at the moment AI launches a rocket.

Thoughts?

Share this post


Link to post
Share on other sites
fnc_launcherHandler = { 
params ["_man"];
private _handler =
_man addEventHandler ["FiredMan", {
	params ["_unit", "_weapon", "_muzzle", "_mode", "_ammo", "_magazine", "_projectile", "_vehicle"];
	if(_weapon == secondaryWeapon _unit)
	then{
		_unit setVariable ["TALLY_launched", true];
		_unit removeEventHandler [_thisEvent, _thisEventHandler];
		systemChat "unit fired launcher";
	};
}];

_handler;
};


fnc_forceLauncherFire = { 
params["_man", "_wantedDir"];

private _handler = [_man] call fnc_launcherHandler;
private _launcher = secondaryWeapon _man;
private _mode = (getArray (configFile >> "CfgWeapons" >> _launcher >> "modes"))#0;
private _fired = (_man getVariable ["TALLY_launched", false]);
private _timer = time +5;

waitUntil {

	if(!isNil "_wantedDir"
	&&{(round _wantedDir) != (round(getDir _man))
	&&{(_timer -  time)>= 1}})
	then{_man setDir _wantedDir};

	_man forceWeaponFire [_launcher, _mode];
	_fired = (_man getVariable ["TALLY_launched", false]);
	((time > _timer) || _fired);
	};

sleep 1;

if!(_fired)then{_man removeEventHandler ["FiredMan", _handler];};
_man setVariable ["TALLY_launched", nil];

true;
};

This one works for me. Basically you got to spam the forceFire-command until he actually completes the action. Then use an eventHandler to kill the loop as soon as the launcher has been fired.
Also it is far far from accurate so you might need to plug in some missile-guidance scripts.

Share this post


Link to post
Share on other sites
On 11/1/2022 at 9:14 PM, _Tally said:

I got the solution for you here

This son could be interested in!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×