How to spawn an entity infront of the player

As the title says, how to spawn an entity infront of a player?


Rate me bad reading.

Yes, but I want to do it with a script (read the section name)


I’m no more in LG because of the immaturity and gayness going there during the last month

local obj = ents.Create(“prop_physics”)
obj:SetPos(ply:GetForward() * 200)
Or something, stick that in a concommand.Add to use the “ply” arg.

I have already tryed the GetForward() function, but it spawns the entity in oblivion instead of infront the player, but meh, I’ll try again

Hm, well technically it should work since it just gets the forward vector and multiplies it. Try adding some units onto the z axis to make it spawn above the ground.

It spawns above the ground, but it is far far away from my position

Lower the multiplier to about, 20

I even tryed to just spawn it at the GetForward() position, it still spawns into the oblivion (in flatgrass I was at the corner of the map and the entity kept spawning on the concrete spawnpoint)

Try printing the value you get from GetForward.

local tr = {}
tr.start = ply:EyePos()
tr.endpos = ply:EyePos() + 95 * ply:GetAimVector()
tr.filter = ply
local trace = util.TraceLine(tr)
ent:SetPos(trace.HitPos) [/lua]
If this is what you wanted then: I never thought you’d ask such a simple thing, Loures.

Gah, why does my code always fail.

Wolfo, that’s with me stopped coding for like 6 months, got a bit rusty


You’re spawning it using a local vector instead of a world vector.

What you might want is:
[lua]local obj = ents.Create(“prop_physics”)
obj:SetPos(ply:GetPos() + (ply:GetForward()*200))

EyePos() is not a local vector.

Ah right, lol.

Yes but GetForward() is.