[TUTORIAL | C#.NET] How to get Aim Positon or Entity (Raycasting + Functions)
I use this to give tasks to peds, or to create explosions, this is a little different from player.getTargettedEntity(), with this method you can be a little more precise on getting AimingPositon and Entity. theres not much info online about it, so have fun.
//get Aim Postion 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)); //AimPostion Result RaycastResult ray = World.Raycast(camPos, camPos + camStuff * 1000, IntersectOptions.Everything); //difference here is "ray.HitCoords" to get positon and "ray.HitEntity'" to get entity EZ
/* Make Owned Explosion on AimPosition [Meaning by 'owned' that u will get WANTED ***** for it] */ World.AddOwnedExplosion(playerPed,ray.HitCoords, ExplosionType.PlaneRocket, 0.5f, 0f);
// Make a spawned ped of yours goTo Position or Any Task at all ped.Task.GoTo(ray.HitCoords);
/* Here im telling my ped to shoot at entity. here you could use player.getTargettedEntity() but that will restrain you on aiming range making you get the entity that you CAN aim at, while using this raycast method you can aim at any range at all and still find the entity */ Function.Call(Hash.TASK_SHOOT_AT_ENTITY, ped, ray.HitEntity, 5000, FiringPattern.FullAuto.GetHashCode());
@preto89 nice tutorial, useful for people making mods
This post is deleted!