Hi, I’ve been working on a map editor for FiveM and recently started porting stuff to C#.
In the following video you can see me flying around in freecam and showing a crosshair indicating if the ray hit anything (based on RaycastResult.DitHit, which seems to be a misspelling of DidHit?) and if so, where it hit (based on RaycastResult.HitPosition). When I click, the current ray is also drawn on the screen. This is all done in C#, and you can see that the HitPosition is always Vector3(0,0,0) (which is the result when it did NOT hit anything).
To show that it works in Lua, I cast the same ray in Lua on mouseclick, and create a checkpoint at the hitPosition that I get from Lua.
Am I fucking up in C# or is this an actual bug?
Relevant code:
(you can be fairly certain that my forwardVector etc are correct, look at the red line in the video)
C#
ray = World.Raycast(_cam.Position, _cam.Position + (forwardVector * 100f), IntersectOptions.Everything);
if (ray.DitHit)
{
Utils.DrawText("+", 0.5f, 0.5f, 0, 255, 0); // Crosshairs
Utils.DrawText("Hit @ "+ray.HitPosition, 0.5f, 0.6f, 0, 255, 0);
}
else
{
Utils.DrawText("+", 0.5f, 0.5f, 255, 0, 0); // Crosshairs
Utils.DrawText("No Hit", 0.5f, 0.6f, 255, 0, 0);
}
On click I fire an event to Lua which is handled by:
AddEventHandler('dev:ray', function(fromx, fromy, fromz, tox, toy, toz)
Citizen.CreateThread(function()
local rayhandle = CastRayPointToPoint(fromx, fromy, fromz, tox, toy, toz, -1, nil, 0)
local statusint, hit, endcoords, surfacenormal, entityhandle = GetRaycastResult(rayhandle)
Citizen.Trace(hit)
Citizen.Trace(endcoords)
CreateCheckpoint(1, endcoords.x, endcoords.y, endcoords.z, endcoords.x, endcoords.y, endcoords.z, 5.0, 255,0,0,255,0);
end)
end)