Jump to content
Sign in to follow this  
Kolmain

Detecting if Unit In Building

Recommended Posts

Whats the best way to check if a unit is in a building or not?

Share this post


Link to post
Share on other sites

Not sure how reliable it is but this is from DayZ:

private["_unit1","_building","_type","_relPos","_boundingBox","_min","_max","_myX","_myY","_myZ","_inside"];

_unit1 = _this select 0;

//_building = _this select 1;

_building = nearestObject [player, "HouseBase"];

_type = typeOf _building;

_relPos = _building worldToModel (getPosATL _unit1);

_boundingBox = boundingBox _building;

//diag_log ("DEBUG: Building: " + str(_building) );

//diag_log ("DEBUG: Building Type: " + str(_type) );

//diag_log ("DEBUG: BoundingBox: " + str(_boundingBox) );

_min = _boundingBox select 0;

_max = _boundingBox select 1;

//diag_log ("Min: " + str(_min) );

//diag_log ("Max: " + str(_max) );

_myX = _relPos select 0;

_myY = _relPos select 1;

_myZ = _relPos select 2;

//diag_log ("X: " + str(_myX) );

//diag_log ("Y: " + str(_myY) );

//diag_log ("Z: " + str(_myZ) );

if ((_myX > (_min select 0)) and (_myX < (_max select 0))) then {

if ((_myY > (_min select 1)) and (_myY < (_max select 1))) then {

if ((_myZ > (_min select 2)) and (_myZ < (_max select 2))) then {

_inside = true;

} else { _inside = false; };

} else { _inside = false; };

} else { _inside = false; };

//diag_log ("isinBuilding Check: " + str(_inside) );

_inside

Share this post


Link to post
Share on other sites

nice one JW, I will have to give that a try!

I use this function here but can be heavy on performance and it's not optimized yet. Works with about 95% accurancy though.

// File Name: InBuilding.sqf
// -------------------------------------------------------------------------------------------
// Version: 0.1 BETA
// Date: 07/01/2013
// Function: Returns if a unit is in a builing
// Author: Mad_Cheese
// -------------------------------------------------------------------------------------------



private ["_unit","_pos","_roofcheck","_bolean"];

_unit = _this select 0;
_pos = getposASL _unit;
_eyepos = eyepos _unit;	
_bolean = false;
_Array = [];
_roof= [_eyepos select 0,_eyepos select 1, (_eyepos select 2) + 20];
_WallFront = [(_eyepos select 0) + (((25)*sin(getdir _unit))), (_eyepos select 1) + ((25)*cos(getdir _unit)),(_eyepos select 2)];
_WallBack = [(_eyepos select 0) + (((-25)*sin(getdir _unit))), (_eyepos select 1) + ((-25)*cos(getdir _unit)),(_eyepos select 2)];
_WallRight = [(_eyepos select 0) + (((25)*sin(getdir _unit + 90))), (_eyepos select 1) + ((0)*cos(getdir _unit)),(_eyepos select 2)];
_WallLeft = [(_eyepos select 0) + (((-25)*sin(getdir _unit + 90))), (_eyepos select 1) + ((0)*cos(getdir _unit)),(_eyepos select 2)];




_roofcheck = (lineIntersectsWith [_eyepos,_roof,_unit,_unit,true]);
if (count _roofcheck == 0) exitwith {
//hintsilent "no roof";
_bolean
};
_wallcheckFront = (lineIntersectsWith [_eyepos,_WallFront,_unit,_unit,true]);
_wallcheckBack = (lineIntersectsWith [_eyepos,_WallBack,_unit,_unit,true]);
_wallcheckRight = (lineIntersectsWith [_eyepos,_WallRight,_unit,_unit,true]);
_wallcheckLeft = (lineIntersectsWith [_eyepos,_WallLeft,_unit,_unit,true]);



if ((_roofcheck select 0) iskindof "building") then {
{
	if ((_x select 0) iskindof "building") then {
		_Array = _Array + [_x select 0];
	};
} foreach [_wallcheckFront,_wallcheckBack,_wallcheckRight,_wallcheckLeft];

if ({_x == (_roofcheck select 0)} count _Array >= 2) then {
	_bolean = true;		
} else {
	_bolean = false;		
};
};

//if (_bolean) then {hintsilent "in building"} else {hintsilent "not in building"};


_bolean;

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
Sign in to follow this  

×