Spawn entity at eyeTrace

So this may sound simple, And it probably is but for the life of me I cannot figure it out.

I Want it so when I spawn an entity(with code) it will spawn at the players looking position and will not be click into the wall. Also if they are looking to far away then just spawn it in front of the player. This is my code right now(does not work correctly)


local eyeTrace = self:GetEyeTrace()

		local spawnPos = eyeTrace.HitPos

		if spawnPos:Distance(self:GetPos()) > 150 then --This is just to make sure it does not spawn miles into the distance
			
			spawnPos = spawnPos:GetNormalized()

			spawnPos:Mul(150)

			spawnPos = self:GetPos() + spawnPos

		end

		local temp = ents.Create("item_base")
		temp:SetPos(spawnPos)
		temp:InitItem(self.inventory[slot].itemName , amount)
		temp:Spawn()

It’s because you’re trying to normalize and use a vector position as a vector direction. This is creating a directional vector from (0, 0, 0) -> aimPos; not plyPos -> aimPos.

Use #GetAimVector to obtain the normalized vector direction of where the player is looking.

Thanks, Now what about the object clipping into the walls etc

You would want to move the entity along the normal of the faces that it’s clipping in. You could use traces to obtain the normals of nearby surfaces.

Oh, I Did not know that, Thanks!