[HELP] [C#] Issue getting closest player

Hello, I’m writing an aim assist script. I usally write my scripts in Lua but C# has much more advanced math features. I have my own custom script handling the Matrix’s and all the math for them. I checked all my code and everything seems right on the math side of things. Can someone point me in the right direction. Thanks

using CitizenFX.Core;
using CustomMath;
using System;
using static CitizenFX.Core.Native.API;

namespace AimAssist
{
    public class AimAssist : BaseScript
    {
        public AimAssist()
        {
            EventHandlers["onClientResourceStart"] += new Action<string>(OnClientResourceStart);
        }

        private void AimLoop()
        {
            while (true)
            {
                Vector3 Source = GetFinalRenderedCamCoord();
                Vector3 Player = GetEntityCoords(PlayerPedId(), true);
                Ped ClosestPed = World.GetClosest<Ped>(new Vector2(Player.X, Player.Y));
                Vector3 Target = GetPedBoneCoords(ClosestPed.Handle, 0x796E, 0f, 0f, 0f);

                Matrix4x4 matrix = Matrix4x4.CreateLookAt(Source, Target);
                float yaw = (float)Math.Atan2(matrix.V13, matrix.V33);
                float pitch = (float)Math.Asin(-matrix.V23);
                float roll = (float)Math.Atan2(matrix.V21, matrix.V22);

                Chat("Roll: " + roll + " Pitch: " + pitch + " Yaw: " + yaw);

                SetGameplayCamRelativeRotation(roll, pitch, yaw);

                Wait(1);
            }
        }

        private void Chat(string message)
        {
            TriggerEvent("chat:addMessage", new
            {
                color = new[] { 255, 0, 0 },
                args = new[] { "[AimAssist]", $"[LOG] " + message }
            });
        }

        private void OnClientResourceStart(string resourceName)
        {
            if (GetCurrentResourceName() != resourceName) return;

            RegisterCommand("aimassist", new Action(() =>
            {
                AimLoop();
            }), false);
        }
    }
}

What you’re after is a Tick. I have converted your code below:

I also changed some of your native calls; the nice thing about C# is that you don’t have to use API. calls, you can use externals; I’ve linked them below if you’d like to research them some more. Without your Math class I cannot build and test this code, but it should work. Reply if you have any troubles.

Thanks for the reply you code worked wonderfully, one issue…

Ped ClosestPed = World.GetClosest<Ped>(new Vector2(Player.X, Player.Y));

…keeps returning null. Any reason on why this would happen?

I’ve never used that specific external before, and I don’t understand what it is doing behind the scenes I’m sorry. You can give this native a try, there is no (working) external for it.

If that doesn’t work, I tend to use this to get the closest ped, but not in a Tick, so I can’t say it will be very efficient, but it should work.

Ped ped = World.GetAllPeds()
    .Where(ped => ped != Game.PlayerPed)
    .OrderBy(pos => pos.Position.DistanceToSquared(Game.PlayerPed.Position))
    .FirstOrDefault();