[HELP] How to spawn a ped infront of an entity?

I tried adding +5 or +10 to the cords but they ended up somewhere like behind the entity, does anyone know?

Take the position and + (fowardvector * 5). Search the natives for the forwardvector call.

so GetEntityForwardLocation(player) * 5 for all the pos or just x?

That’s a vector3 isn’t it? You take playerpos + (playerforward * 5).

You can take player.x + (forwardX *5)
Then do y same.
Then use player Z or if you’re fancy use the (getgroundZ) which takes an x,y.

Forwardvector is like 1 unit offset in front of you. Or just google the forward vector native people use it for placing cars/peds. I’ll post the actual code when I’m not on mobile.

1 Like

Thank you so much bro it worked!

so I am trying to make the animals I am spawning looking at my direction when they spawn.

I tried to get my heading first

local heading = GetEntityHeading(player)

then for the createPed line I used ‘-heading’ instead of 0… and it almost worked, they are sort of looking at my direction but at a weird angle, do you know if I can do this a better way?

360 - heading :slight_smile:

Player heading - 180 (I’ll check the code I use but I think that’s it). 360 spins them back to the same spot.

note the 360 -, not - 360. order of operands in subtraction matters.

1 Like

You’re right, I didn’t read your code correctly. However, your code doesn’t work regardless. If your heading is 163, 360 - 163 = 197. that’s not the opposite direction. The ped won’t be facing you.

1 Like

Yep, I just would like to confirm for future people searching the forum for answers:

using

local heading = GetEntityHeading(player)

and

local fish = CreatePed(4, hex, playerPos.x + (frontx * 10), playerPos.y + (fronty * 10), playerPos.z + 1, (heading - 180), true, false)

spawns a ped facing towards me 100% of the time.

I tried using (360 - heading) but it never got the ped to face towards player.

Good luck everyone and thank you both :slight_smile:

I don’t think you’re suppose to technically go negative (say heading is 90, it will go -90, the system seems to understand that’s 270). So this is probably the proper way, and it’s easy to follow/make changes. This will flip your heading.

local heading = GetEntityHeading(GetPlayerPed(-1))
local newheading = 0

if(heading >= 180) then
newheading = heading - 180.0
else
newheading = heading + 180.0
end

SetEntityHeading(GetPlayerPed(-1), newheading)

You can also use that to set the peds heading to facing you.