Jump to content

Make ped follow player


alexguirre

Recommended Posts

I've been trying to achieve this but it never ends up working well, the best code I get is below but the ped sometimes gets stuck in the ladders(he stays looking at the ladder) and the movement of the ped isn't smooth:

 


	Ped ped = new Ped(player.GetOffsetPosition(new Vector3(0f, -4f, 0f)));

	GameFiber.Sleep(1000);

	NativeFunction.CallByName<uint>("SET_PED_PATH_CAN_USE_CLIMBOVERS", ped, true);

	NativeFunction.CallByName<uint>("SET_PED_PATH_CAN_USE_LADDERS", ped, true);

	NativeFunction.CallByName<uint>("SET_PED_PATH_CAN_DROP_FROM_HEIGHT", ped, true);

	while (!Game.IsKeyDown(Keys.Y))

	{

	    GameFiber.Yield();

	    if (Vector3.Distance(player.Position, ped.Position) > 2.0f)

	    {

	        Vector3 p = player.GetOffsetPosition(new Vector3(0f, -1.25f, 0f));

	        Task t = ped.Tasks.FollowNavigationMeshToPosition(p, player.Heading, 1.0f);

	        while (Vector3.Distance(player.Position, ped.Position) > 5.0f)

	        {

	            GameFiber.Yield();

	            if (Vector3.Distance(p, ped.Position) < 2.0f) break;

	            if (t.Status == TaskStatus.Interrupted) break;

	            if (!t.IsActive) break;

	        }

	        ped.Tasks.Clear();

	    }

	}


 

 

 

Does anyone have a better code?

Edited by alexguirre
Link to comment
Share on other sites

You need to provide some more WaitTime I think. Maybe you could try Sleeping the GameFiber for 500 ms or so every tick?

Also, although I'm not sure in this case, I don't recommend clearing the ped's tasks every time.

My YouTube: Click here. 

My Discord Server - https://discord.gg/0taiZvBSiw5qGAXU

Useful post? Let me and others know by clicking the Like button.
Check out my many script modifications! 
Having issues? LSPDFR Troubleshooter by Albo1125.

Link to comment
Share on other sites

4 hours ago, Albo1125 said:

You need to provide some more WaitTime I think. Maybe you could try Sleeping the GameFiber for 500 ms or so every tick?

Also, although I'm not sure in this case, I don't recommend clearing the ped's tasks every time.

That makes it work a bit smoother. When it gets stuck in front of a ladder if I clear the tasks and make the loop again he climbs it, I suppose it gets the vector3 in midair and the AI doesn't know what to do. I might check if the player is climbing d wait for it to finish and tanhen get the vector3 and assign the task.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...