View Full Version : [EDITING] Checking a units speed using speed vectors

04-01-2012, 05:19 PM
Though arma knows speed (http://community.bistudio.com/wiki/speed) to return a unit's movement speed, this unfortunately only returns km/h, making it incomparable to getSpeed (http://community.bistudio.com/wiki/getSpeed) which returns units per second.

So if we wanted to check if a unit is moving at a certain speed and then want something to happen we have to use velocity (http://community.bistudio.com/wiki/velocity).:

In the example, a unit sets off a trigger that activates nul=[thisList] execVM "speedcheck.sqf"

_unit = _this select 0;
_speed = sqrt ( (velocity _unit select 0)^2 + (velocity _unit select 1)^2 + (velocity _unit select 2)^2 );
//triangulation reduces the array velocity returns to a single number

_allowedSpeed = (_unit getSpeed "Normal");
//we define the allowed movement speed (for infantry "Normal" is a jog at 4.0)

if (_speed > _allowedSpeed) then {
_running = 1;
//the unit is running
} else {
_running = 0;
//the unit is going slower than jogging};

if (f_var_debugMode == 1) then { //using the default F2 Debug parameter
player sidechat format ["_speed:%1,_allowedspeed=%2", _speed, _allowedspeed];
player sidechat format ["running:%1",_running];};

Placing this trigger in a mission using the F2 framework you can use debug mode to finetune the allowedspeed to your leisure. E.g. _allowedSpeed = ((_unit getSpeed "Normal")-0.5)) reduces the allowed speed to everything slower than a crouched jog.

Thanks to Nullkigan for coming up with the codestuff.

04-01-2012, 06:19 PM
Not sure that will work because "this (http://community.bistudio.com/wiki/this)" in activation will return false, not a unit as you want.

Here's an example I whipped up for velocity sensitve IEDs:

Activated by: Blufor, Present, Repeated
Size: Rectangular, 5, 1
Timer: 0, 0, 0, countdown
On activation: if (isServer) then { {nul = [thisTrigger, _x,"B_30mm_HE",0.2,2] execVM "scripts\speedtrap.sqf";} forEach thisList; };

Which says that for each blufor present in the trigger area, run the script passing the name of the trigger, the name of the unit and the values "B_30mm_HE", 0.2 and 2 to the script. (Explosive device, chance of being set off, radius of placement)

The script then looks like this (I doubled up on isServer, but it pays to be careful with locality):

private ["_unit","_speed","_allowedSpeed","_trigger","_ammotype","_chance","_variance","_variancey","_rand"];

if (isServer) then {
_trigger = _this select 0;
_unit = _this select 1;

if (!rps_param_ieds) exitWith {_trigger setpos [-200,-200];}; //disable IEDs by param, ignore the rest of this isServer loop!

_speed = sqrt ( (velocity _unit select 0)^2 + (velocity _unit select 1)^2 + (velocity _unit select 2)^2 ); // getspeed is in different units
_allowedSpeed = 3.9; //(_unit getSpeed "Normal")-0.1;
// For an infantry unit, slow/norm/fast are 2.7/4/6.666 or thereabouts

// Check speed
if (_speed > _allowedSpeed) then {

_ammotype = _this select 2;
_chance = _this select 3;
_variance = _this select 4;

if (isNil "_ammotype") then {_ammotype="B_30mm_HE";};
if (isNil "_chance") then {_chance=0.20;};
if (isNil "_variance") then {_variance=1;};

_chance = (_chance * 100);
_variancey = random _variance - (_variance/2);
_variance = random _variance - (_variance/2);

_rand = random 100;
if (_rand < _chance) then {
_ammotype createVehicle [(getPos _unit select 0) + _variance,(getPos _unit select 1) + _variancey]; // place bomb within radius
_trigger setpos [-200,-200]; // exploded, so no longer need trigger
} else {
if ( _rand > (98 min (100-(_chance/10))) ) then { // each time it is run over but not exploded, there is a chance of accidentally defusing the bomb, min 2% but relative to the chance of the bomb actually going off
_trigger setpos [-200,-200]; //complete dud; with _chance of 0.15 there is a 15% chance of detonation and a 2% (minimum) chance of defusal. 0.25 = 25%, 2.5%