Creating a raycast, targeted at crosshair position

Hello.
I’ve been doing a project that requires me to raycast where I’m aiming.
The raycast works fine, and I’ve been using the gameplay cam as my way of doing this.
It’s very unprecise, and does not lead to the result I want.

Is there some way to get the coords of the crosshair, so that I can cast my ray starting from that?
Thank you.

Don’t know what your currently using to raycast but this is what I use and it’s pretty precise to me: https://runtime.fivem.net/doc/reference.html#_0x2975C866E6713290

Thanks, but the range is just super short on that function - any other way?

That isn’t even the main issue. It only works on non-moving vehicles, not moving ones.

2 Likes
local function GetEntInFrontOfPlayer(Distance, Ped)
    local Ent = nil
    local CoA = GetEntityCoords(Ped, 1)
    local CoB = GetOffsetFromEntityInWorldCoords(Ped, 0.0, Distance, 0.0)
    local RayHandle = CastRayPointToPoint(CoA.x, CoA.y, CoA.z, CoB.x, CoB.y, CoB.z, 10, MyPed(), 0)
    local A,B,C,D,Ent = GetRaycastResult(RayHandle)
    return Ent
end

Hi this casts a ray from the front of an entity at distance X. Works on moving vehicles.

1 Like

Thanks, but that’s kind of what I had to start with. This does not focus on where you’re aiming with you weapon, and your crosshairs position though.
If I just use:

local bool, entity = GetEntityPlayerIsFreeAimingAt(PlayerId())

And print the resulting entity in the console, I get everything showing up just fine. It finds the detected entities, and prints out their ID.
It does not work however, later in the script - where I try to get the speed of said Entity, the model and etc.
That only works using a raycast, which confuses me.

Had a bit of an issue, and now speed works perfectly for every vehicle.
However - Model, and platetext do not work unless the vehicle aimed at is spawned by me.
What could that be?

May wanna post your code so people can actually see what may be wrong instead of having us all guess.

1 Like

It’s picking up the Peds Entity. You can use the natives to check if the entity is a ped, and then get their vehicle id and you will be and to get the plate and car model