Jump to content
Sign in to follow this  
download500

How to spawn a cop a ways out and make them drive to me, vs spawning at my location?

Recommended Posts

I'm creating a mod that will allow you to pre-dispatch backup to a location before you arrive in case a responding unit can get there first.

I also am creating the ability to dispatch units to points as nothing more than traffic control and have them stand by until you dismiss them.

 

I understand how to spawn them at the location, but how do I spawn them a distance out and make them drive to it?

 

This is my current code:

 

  if (Game.LocalPlayer.Character.Position.DistanceTo(myPed.Position) > 50.0F && Game.LocalPlayer.Character.Position.DistanceTo(caller.Position) > 50.0F)
                {

                    int cophelps = new Random().Next(1, 5);

                    if (cophelps == 1)
                    {



                        int copFindsFirst = new Random().Next(1, 2);
                      

                        if (copFindsFirst == 2)

                        {
                            Game.DisplaySubtitle("Other Unit: I've located the subject, I'm in pursuit.", 5000);
                            Functions.AddPedToPursuit(this.pursuit, this.myPed);
                            Functions.RequestBackup(myPed.Position.Around(5f), LSPD_First_Response.EBackupResponseType.Code3, LSPD_First_Response.EBackupUnitType.LocalUnit);
                            cop1 = new Ped("S_M_Y_Sheriff_01", myPed.Position, 1f);
                            copcar1 = new Vehicle("SHERIFF", myPed.Position, 5f);
                            copcar1.IsSirenOn = true;


                        }
                        else
                        {
                            Game.DisplaySubtitle("Other Unit: I'm out with the caller.", 5000);
                            cop1 = new Ped("S_M_Y_Sheriff_01", caller.Position, 1f);
                            copcar1 = new Vehicle("SHERIFF", caller.Position, 5f);
                            copcar1.IsSirenOn = true;



                        }

                    }
                }
            }

 

Edited by download500

Share this post


Link to post
Share on other sites

The problem with that though is I need them to be on the scene behaving calmly, not with guns drawn, and I want them to stay there until I tell them to go away.

With the backup function, the only way to get them with a siren is to call them code 3, which has them go guns drawn, and they don't stay long.

 

Is there a function that will let me reference the nearest cop, or all cops within a range as a PED so that I can give them tasks/make them stand indefinitely?

Share this post


Link to post
Share on other sites

What you want to do is get a random spawn point with World.GetNextPositionOnStreet(Game.LocalPlayer.Character.Position.Around(250)). You can change the 250 to however many metres you want.

Then spawn your cop car, then spawn the cop (I usually do right next to the vehicle), and warp him into the vehicle. At that point, you can call copPed.Tasks.DriveToPosition to make him drive to a Vector3 near you. You can use whatever driving flags you want, but I'd use either Normal or Emergency, and turn on the lights/siren for him if desired.

You could also get a list of peds near you and filter that for cops, but its probably easier (and more efficient) just to create the cop.

I did this with my Unknown Trouble callout, spawning a homicide detective. Let me know if you need more help.

Share this post


Link to post
Share on other sites

After a lot of trial and error, I found the best way to get them to stop near you is to set them to DriveToPosition of the player's ped, and use the overload that lets you specify an accepted distance. This seems to work a lot better than telling them to drive to a random position near the ped (can quite easily be a place thats not reachable). For example:

emsMedic.Tasks.DriveToPosition(target, 35, VehicleDrivingFlags.Emergency, 30f).WaitForCompletion();
 The 30f here is the acceptable range - as soon as my medic gets within this range he stops driving and the thread continues.

Share this post


Link to post
Share on other sites
1 hour ago, Darkmyre said:

After a lot of trial and error, I found the best way to get them to stop near you is to set them to DriveToPosition of the player's ped, and use the overload that lets you specify an accepted distance. This seems to work a lot better than telling them to drive to a random position near the ped (can quite easily be a place thats not reachable). For example:

 


emsMedic.Tasks.DriveToPosition(target, 35, VehicleDrivingFlags.Emergency, 30f).WaitForCompletion();

 The 30f here is the acceptable range - as soon as my medic gets within this range he stops driving and the thread continues.

Omg why have I not thought of this! I had a problem with one of my callouts and the driver of a vehicle would just run over a ped I had told it to go to.

It got so rage inducing that I just ended up leaving my computer.

Share this post


Link to post
Share on other sites
5 minutes ago, tanu1215 said:

Omg why have I not thought of this! I had a problem with one of my callouts and the driver of a vehicle would just run over a ped I had told it to go to.

It got so rage inducing that I just ended up leaving my computer.

lol I feel your pain... for me it was stupid EMS ram-raiding their way through the scene of my accident callout, before treating the peds in the accident and the others they'd just run down. Amusing but so frustrating at the same time lol

Share this post


Link to post
Share on other sites
14 hours ago, Darkmyre said:

After a lot of trial and error, I found the best way to get them to stop near you is to set them to DriveToPosition of the player's ped, and use the overload that lets you specify an accepted distance. This seems to work a lot better than telling them to drive to a random position near the ped (can quite easily be a place thats not reachable). For example:

 


emsMedic.Tasks.DriveToPosition(target, 35, VehicleDrivingFlags.Emergency, 30f).WaitForCompletion();

 The 30f here is the acceptable range - as soon as my medic gets within this range he stops driving and the thread continues.

@Darkmyre wins.

I didn't even realize that DriveToPosition had that extra overload. I was picking a random spot around the player, and calling World.GetNextPositionOnStreet to ensure it wasn't an unreachable spot.

Share this post


Link to post
Share on other sites

I'm using the following code, but for some reason it spawns the cop instantly on top of me, versus having him spawn 1000f feet out and drive to me. What am I doing wrong?

 

                            Game.DisplaySubtitle("David 1 en route", 5000);
                            cop1 = new Ped("S_M_Y_Sheriff_01", Game.LocalPlayer.Character.Position, 1000f);
                            v1 = new Vehicle("SHERIFF", cop1.Position, 5f);
                            cop1.WarpIntoVehicle(v1, -1);
                            v1.IsSirenOn = true;
                            v1.IsSirenSilent = true;
                            b1 = cop1.AttachBlip();
                            b1.Order = 100;
                            //b1.Color = Color.Yellow;
                            cop1.Tasks.DriveToPosition(Game.LocalPlayer.Character.Position, 35, VehicleDrivingFlags.Emergency, 30f).WaitForCompletion();
                            cop1.Tasks.LeaveVehicle(LeaveVehicleFlags.LeaveDoorOpen);
                            Game.DisplaySubtitle("David 1 arrival", 5000);

 

UPDATE: Got it working! Had to use  cop1 = new Ped("S_M_Y_Sheriff_01", World.GetNextPositionOnStreet(Game.LocalPlayer.Character.Position.Around(250)), 1000f);

Share this post


Link to post
Share on other sites
1 hour ago, download500 said:

UPDATE: Got it working! Had to use  cop1 = new Ped("S_M_Y_Sheriff_01", World.GetNextPositionOnStreet(Game.LocalPlayer.Character.Position.Around(250)), 1000f);

You've got 1000 as the heading right now. Heading is 0 to 359, if I'm not mistaken, so you can just set that to 0, and put 1000f where the 250 is. Keep in mind though, the unit is metres, not feet. The 'f' on the end converts it to a float.

So 1000 metres would be 1km, or 0.62 miles. So he would spawn at any point within a 1km radius...the distance is as the crow flies, so the actual DRIVING distance on the roads would be longer. 250 meters is probably good enough.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×