Spawning entity directly in front of player?

This is the code I’m using:

[lua] N = ents.Create(“weapon_ak47”);
N:SetPos(Vector(_ply:GetEyeTrace().HitPos.x, _ply:GetEyeTrace().HitPos.y, _ply:GetPos().z + 10));[/lua]

What I want it to do is spawn the entity directly in front of the player in the direction he’s looking at, but I don’t want to spawn it right where he’s looking.

I don’t understand how to do this

OH, you want it to spawn not where the player is looking at but in front of him, hmm.
try

[lua]
local pos = ply:GetPos()
N:SetPos(pos + Vector(10,0,20))
[/lua]

Basicly you get the player position in the world and you add to it a vector to spawn infront of player.

Note: That code isn’t perfect, if you try to spawn it at a wall the gun will probably get stuck, or if you try to spawn it in a mountain, it may get stuck too, to be sure it wouldn’t get stuck when aiming at some small stuff, It adds 20 to the player vertical pos, so the gun spawns in air.

That’s basicly what you should do, ofc you may want to get more code to be sure it only spawns in avaible space, maybe do a trace from the player pos to where the gun will be spawned to retrieve a error message to the player if it hits anything or do any workaroud you want.

Edit:
Just noticed how this wouldn’t spawn in the players exact front, let me try to come with something.

player:GetForward() ?

That makes it easier. thanks.

And I was here trying to make a equation to set the position based in the player view’s horizontal angle.
I sure should have of searched the wiki better.