[C#] Get ped I'm aiming

Hello, I’m tring to get the ped that I’m aiming, I tried with that but is not working I probably did something wrong here:

       public void GetPed()
        {

            int entity= 0;


            API.GetEntityPlayerIsFreeAimingAt(-1, ref entity);


            Debug.WriteLine(entity.ToString());


            //SE L'ENTITA' E' UN PED
            if (API.IsEntityAPed(entity) == true)
            {
               
                //SE L'ENTITA' E' VIVA
                if(API.IsEntityDead(entity) == false){

Ped ped = entity as Ped;

Debug.WriteLine(ped.Model.ToString());

                }


            }
 

        }
local aiming, targetPed = GetEntityPlayerIsFreeAimingAt(PlayerId())
 if aiming then
  if DoesEntityExist(targetPed) and IsEntityAPed(targetPed) then

This is how it’s done in lua. Not sure if this helps you though.

I already seen this in lua… My problem is that GetEntityPlayerIsFreeAimingAt have 2 argoument in c# and for some reason I did something wrong and I can’t get the entity ID , stay always as 0 and I also get some error.

I’m not able to translate this lua code in C#. I will add DoesEntityExist(targetPed) to be sure that exist, but I have not fixed . Thx anyway gadolinium. If someone find the answer thx very much :).

I am not sure, but I think -1 is not your own player… Try using this instead:

https://runtime.fivem.net/doc/reference.html#_0x4F8644AF03D0E0D6

1 Like

Thx Flatracer with this we solved the problem the ID requested is the local ID, sometimes when ask the int Player you should put the server id and sometimes the local , is not clear. Anyway the only part missing is now how to convert this entity into a Ped. If anyone need the code is this for now:

        public void GetPed()
        {

            int entity = 0;

            bool aimEntity = API.GetEntityPlayerIsFreeAimingAt(API.PlayerId(), ref entity );

            //player is aiming
            if (aimEntity == true)
            {
                //the entity exist (is aiming an entity)
                if (API.DoesEntityExist(entity ) == true)
                {

                    if (API.IsEntityAPed(entity ) == true)
                    {
                        if (API.IsEntityDead(entity) == false)
                        {

                            Debug.WriteLine("Aiming to alive Ped " + entity.ToString());

                        }
                        else
                        {
                            Debug.WriteLine("Aiming to a death Ped " + entity.ToString());
                        }

                    }
                    else {

                        if (API.IsEntityDead(entita) == false)
                        {
                            Debug.WriteLine("Aiming to an alive entity (not Ped)  " + entity.ToString());

                        }
                        else
                        {
                            //miro a una entita' che non e' un ped
                            Debug.WriteLine("Aiming to a death entity (not Ped)  " + entity.ToString());
                        }

                    }

                }
                else {
   
                    Debug.WriteLine("Is aiming but not an entity'");
                }

            }
            else
            {
                Debug.WriteLine("false");
            }

Just need to know how to convert that entity now to a ped.

You normally use the Server ID if you do something in a Server Script, Client Scripts are most of the time using the Client ID.

1 Like

A perfect I confused probably sending from the client to the server “the server ID to have a list of the server ID” :smiley: .
But I see now most of the times you use the local ID on client side :D! Is more clear now :D.

1 Like

I am glad I was able to help

1 Like

Thx again , for the last part if someone need (use the entity as a ped), you must use this after if (API.IsEntityAPed(entity ) == true) :

Ped targettedPed = Game.Player.GetTargetedEntity() as Ped;

So you will have a ped

when you have an entity, you can use the model of the entity for know what is it : entity.Model.isPed() or entity.Model.isVehicle() ect…

when you know what it is you can use the handle of the entity for instance the wanted object (in your case it’s a ped) :
Ped p = new Ped(entity.Handle);

ps: don’t forget that the handle are only client side and not networked. if you want to network it you have to use the native PedToNet and NetToPed

1 Like

Thx very much 304bl, I have a question, the conversion Ped p = new Ped(entity.Handle); is better than Ped ped = entity as Ped; ? I mean that Handle thing help in some way? Just to know to improve my skill and the code :grin:.

Thx very much :blush::v:

both are working so i won’t say one is better then other , but from my experience , i prefer to work with the handle for few reason :

-it’s a int, easy and not heavy to store into a tab
-almost everything from the game engine (native function) work with the handle.

1 Like

A ok thx very much :D! This could help for the mysql part :D. Soo entity.Handle could be good to store data and p will be a referece for the ped to use function :D.

no lol , handle shouldn’t be store in any database , for the simple reason that when you close the game and start it again the handle will change for everything , a handle is an ID who is given by the game to an object you create , when that object is delete by you or the game the handle is release and will be use by another object who need to be create.

when i mean store it , it was in your script in a tab of ped you create or liist for use them.

ps : p is just a short name i use you can call it whatever you want.

1 Like

A ok, thx very much :grin:.