How do I make it so that a scripted entity is in front of a player at all times?

Here’s a quick and dirty example of what I mean.

I’m starting to wrap my head around Garry’s Mod’s Lua. While I’m not finding this too hard, I’m unfortunately a terrible mathematician. I’ve been searching Google, thinking, and experimenting for hours, but I just can’t find out how to properly do it. As such, I’m not necessarily asking for a working snippet, but any useful pointers would be appreciated as well.

Thanks in advance.

Ent:SetPos(targPlayer:GetPos() + targPlayer:GetForward() * 25)

I’ve tried this. The entity always ends up stuck below me.

Then you’re doing something wrong. Post your code.

You need to use clienside drawmodel or something clientside to place entity in front of view needs to use trace of view. With this you can drawmodel only for player. If you want to draw i to all players, you can use parent to player, cos it parent to player view. Get player view vector with player:GetEyeTrace() scale this vector to get entity in front of you, then ent:SetParent(player).
First method needs to call by hook “PostPlayerDraw”
Second needs call only one time.

function ENT:Think()
    local ent = self:GetNetworkedEntity("UsedBy")
    if ent:IsPlayer() then
        self:SetPos(ent:GetPos() + ent:GetForward() * 25)

Congratulations on your 1500th post, by the way.

try increasing the multiplier to a greater value.
Replace GetPos with EyePos.

Using EyePos worked. Thanks a lot.

I’ve noticed that this disables ENT:PhysicsCollide and collisions in general, however. I’m assuming this is because of the continuous updating of the position, but correct me if I’m wrong. Is there a workaround for this?

You are constantly setting its position and constantly grabbing the NWEntity. That is an expensive function to run at the best of times.

Try using parenting:

Thanks for the 1500th post thing :stuck_out_tongue: I was counting down the posts and then forgot to make a point about it :v:

And the problem was that you were setting the entities positions relative to it’s self, not the player. Kuro Light pointed it out :slight_smile:

In terms of collisions, you are setting the position, so you are not actually colliding with it. Valve disabled collisions for an object inside another to avoid ‘crazy physics’.

And finally, use SetParent. It’s cleaner, you will only have to call it the once.