Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

C# How to check if a ped is visible?


cantRegister
 Share

Recommended Posts

cantRegister

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

  • 2 months later...
Alwayskaffa

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

Alwayskaffa

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

  • 2 weeks later...
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 by ech3lon
  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.