cantRegister Posted April 13, 2016 Share Posted April 13, 2016 Hello, I can't find a way to check if a ped is visible for the player. I'm running out of options and it's already been 3hours I'm looking for something after endless tests. Hell, I'm even wondering why there isn't a simple native for that. Let's suppose my ped is behind a car, behind a building or even under a bridge, in the player's eyes, he can't see him, right? Is there a work-around or a method to do so? I'd like to return a bool value such as public bool isVisible(Ped ped):{ bool visible = false; //logic here return visible;} What I've tried so far : 1) Raycasting can only return HitCoords and you have to be very sharp with the aiming in order to detect any ped. RaycastResult r = World.Raycast(player.Position, maxPos, IntersectOptions.Peds1); 2) Using the native CELL_CAM_IS_CHAR_VISIBLE_NO_FACE_CHECK which worked perfectly but can't return anything above 20meters. Function.Call(Hash.CELL_CAM_IS_CHAR_VISIBLE_NO_FACE_CHECK, ped) Link to comment Share on other sites More sharing options...
alloc8or Posted April 13, 2016 Share Posted April 13, 2016 You could use IS_ENTITY_ON_SCREEN. Link to comment Share on other sites More sharing options...
Alwayskaffa Posted June 14, 2016 Share Posted June 14, 2016 You could use IS_ENTITY_ON_SCREEN. would only be 180 degrees though. Use ENTITY::HAS_ENTITY_CLEAR_LOS_TO_ENTITY(PLAYER::PLAYER_PED_ID(), ped, 17); Link to comment Share on other sites More sharing options...
GRANDHEIST Posted June 15, 2016 Share Posted June 15, 2016 Maybe ENTITY::IS_ENTITY_VISIBLE Link to comment Share on other sites More sharing options...
Alwayskaffa Posted June 15, 2016 Share Posted June 15, 2016 No cause a entity would stil show up visible for example behind a wall where you cant look directly at just because you loaded the map This is the only right one; ENTITY::HAS_ENTITY_CLEAR_LOS_TO_ENTITY(PLAYER::PLAYER_PED_ID(), ped, 17) Maybe ENTITY::IS_ENTITY_VISIBLE Link to comment Share on other sites More sharing options...
g3o0or Posted June 15, 2016 Share Posted June 15, 2016 bool isVisible = !ENTITY::IS_ENTITY_OCCLUDED(ped); Link to comment Share on other sites More sharing options...
ech3lon Posted June 24, 2016 Share Posted June 24, 2016 (edited) HAS_ENTITY_CLEAR_LOS_TO_ENTITY(PLAYER::PLAYER_PED_ID(), ped, 17); This only checks for obstructions between two entities, meaning you can be standing "behind" someone and this will still return true. bool isVisible = !ENTITY::IS_ENTITY_OCCLUDED(ped); This native is very inconsistent. Sometimes it works fine, but quite often (especially in buildings) it just returns the wrong values. For example, sometimes you are in a room below an entity and IS_ENTITY_OCCLUDED returns "false". IS_ENTITY_ON_SCREEN Works fine I think, but obviously only works if your origin is the player ped. My Solution, a combination of some math and HAS_ENTITY_CLEAR_LOS_TO_ENTITY for checking occlusion. Might not be the fastest but it is very accurate, even inside interiors. /// <summary> /// Determine the dot vector product between source and target ped /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns>float in range -1.0 to 1.0, negative value if source is behind target, positive value if source is in front of target, 0 if source is orthogonal to target, 1 if directly in front of target, -1 if directly behind target</returns> public static float getDotVectorResult(Ped source, Ped target) { if (source.Exists() && target.Exists()) { Vector3 dir = (target.Position - source.Position).Normalized; return Vector3.Dot(dir, source.ForwardVector); } else return -1.0f; } /// <summary> /// Determine if any of given peds is in line of sight to the player /// </summary> /// <param name="peds">List of peds to check for</param> /// <param name="minAngle">the value of the dot product at which a ped is considered not in LoS</param> /// <param name="withOcclusion">true if occlusion check should be included, false otherwise</param> /// <param name="includeDead">true if dead peds should be included, false otherwise</param> /// <returns>true if at least one ped was in los, false otherwise</returns> public static bool IS_ANY_PED_IN_LOS(List<Ped> peds, float minAngle, bool withOcclusion = true, bool includeDead = false) { foreach (Ped ped in peds) { if (ped.Exists()) { if (ped.IsDead && !includeDead) { continue; } if (withOcclusion) // with obstacle detection { if (HAS_ENTITY_CLEAR_LOS_TO_ENTITY(ped, Game.Player.Character)) // No Obstacles? { float dot = getDotVectorResult(ped, Game.Player.Character); if (dot > minAngle) // Is in acceptable range for dot product? { return true; } } } else // without obstacle detection { float dot = getDotVectorResult(ped, Game.Player.Character); if (dot > minAngle) // Is in acceptable range for dot product? { return true; } } } } return false; } So for example, if I want to know whether some Ped from a list of Peds can see the player: IS_ANY_PED_IN_LOS(World.GetNearbyPeds(Game.Player.Character, 40.0f).ToList(), 0.55f) Where 0.55 is the value of the dot product that detemines the angle at which we accept the player to be in line of sight of the ped. E.g.: between 0 and 1 for +-90 degrees in front, or between 0 and -1 for +-90 degrees behind. A value between 0.55 and 0.65 gives a reasonable acceptable cone of vision in front of the origin. Note: If you want to specify the minimum angle between entities in Degrees instead of giving direct value for the dot product, then you need to compute arccos as well and convert radians to degrees. Additionally it might be useful to make sure Z coords are within a certain range (e.g. ped can't see other ped if it is far above or below him). So as additional check, for example: Math.Abs(source.Position.Z - target.Position.Z) < 2.5f Edited June 24, 2016 by ech3lon ins1de and Jitnaught 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now