Pandassaurus Posted July 18, 2015 Share Posted July 18, 2015 (edited) Hi everyone, I was wondering how raycast results work in c#. I played around with them a little and found them to be very buggy. Could anyone explain/ link me to an explanation? My goal is to get the coords of the surface you're looking at, whatever you hit, however far. Thanks for the help? Edited July 18, 2015 by Pandassaurus Link to comment Share on other sites More sharing options...
lulzmodder Posted July 18, 2015 Share Posted July 18, 2015 Are you asking for an explanation on raycasting or how to use it in GTA V? Link to comment Share on other sites More sharing options...
Pandassaurus Posted July 18, 2015 Author Share Posted July 18, 2015 Are you asking for an explanation on raycasting or how to use it in GTA V? I understand what they are, but want to know how to use it in gta Link to comment Share on other sites More sharing options...
lulzmodder Posted July 18, 2015 Share Posted July 18, 2015 I am finding them buggy as well. Link to comment Share on other sites More sharing options...
Pandassaurus Posted July 19, 2015 Author Share Posted July 19, 2015 (edited) I got them to work! I found an open source mod (thanks to Enumerator) that used them in vb and translated it over to C#. Vector3 camPos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD); Vector3 camRot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT); float retz = camRot.Z * 0.0174532924F; float retx = camRot.X * 0.0174532924F; float absx = (float) Math.Abs(Math.Cos(retx)); Vector3 camStuff = new Vector3((float) Math.Sin(retz) * absx * -1, (float) Math.Cos(retz) * absx, (float) Math.Sin(retx)); RaycastResult ray = World.Raycast(camPos, camPos + camStuff * 1000, IntersectOptions.Everything); Ped ped = World.CreateRandomPed(ray.HitCoords); It works pretty well, except that it does crash quite often. I'm not sure why this is, but am looking into it. edit: I think the crashing had more to do with the ped spawning that i used for testing than with the actual rays. Edited July 20, 2015 by Pandassaurus Enumerator 1 Link to comment Share on other sites More sharing options...
identitymatrix Posted July 28, 2015 Share Posted July 28, 2015 (edited) I got them to work! I found an open source mod (thanks to Enumerator) that used them in vb and translated it over to C#. Vector3 camPos = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_COORD); Vector3 camRot = Function.Call<Vector3>(Hash.GET_GAMEPLAY_CAM_ROT); float retz = camRot.Z * 0.0174532924F; float retx = camRot.X * 0.0174532924F; float absx = (float) Math.Abs(Math.Cos(retx)); Vector3 camStuff = new Vector3((float) Math.Sin(retz) * absx * -1, (float) Math.Cos(retz) * absx, (float) Math.Sin(retx)); RaycastResult ray = World.Raycast(camPos, camPos + camStuff * 1000, IntersectOptions.Everything); Ped ped = World.CreateRandomPed(ray.HitCoords); It works pretty well, except that it does crash quite often. I'm not sure why this is, but am looking into it. edit: I think the crashing had more to do with the ped spawning that i used for testing than with the actual rays. That cam stuff is rotation to direction. For some reason, enumerator uses natives for everything despite it being in the scripthookvdotnet api. Here is a reusable func for getting direction from a rotation vector Public Shared Function RotationToDirection(ByVal Rotation As GTA.Math.Vector3) As GTA.Math.Vector3 Dim rotZ As Double = DegreeToRadian(Rotation.Z) Dim rotX As Double = DegreeToRadian(Rotation.X) Dim multiXY As Double = Math.Abs(Convert.ToDouble(Math.Cos(rotX))) Dim res As GTA.Math.Vector3 res.X = Convert.ToDouble(-Math.Sin(rotZ)) * multiXY res.Y = Convert.ToDouble(Math.Cos(rotZ)) * multiXY res.Z = Convert.ToDouble(Math.Sin(rotX)) Return res End Function Dim rres As RaycastResult Dim cpos As Vector3 = GameplayCamera.Position rres = World.Raycast(cpos, cpos + RotationToDirection(GameplayCamera.Rotation) * 1000, IntersectOptions.Everything) If rres.DitHitAnything Then msg(rres.HitCoords.ToString) End If Edited July 28, 2015 by TheVideoVolcano Link to comment Share on other sites More sharing options...
Tieentjeee Posted March 22, 2016 Share Posted March 22, 2016 Thank you very much, just wanted to let you know, even if its months old by now, it still gets read by people (like me) who find it very useful! Link to comment Share on other sites More sharing options...
mockba.the.borg Posted March 23, 2016 Share Posted March 23, 2016 Hi everyone ... I am having trouble with the Ray Cast due to the range it intersects object. It seems I can only match some vehicles and peds up to about 32 meters away. Other vehicles, like jets, the blimp and the metrotrain, for example, will match up to 1000 meters away. I though that the raycast would match anything as long as it is visible, is it true? Or configurable? Thanks for any help. Mockba. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now