Lock viewpoint to active player bone

Hey there,
I recently started trying out Lua and for the most part it makes sense and I seem to be understanding it. However what I want to try is completely evading me.

What I am effectively trying to do is get the player’s viewpoint to lock on to the ‘eyes’ bone of the player model - rather like the NPC Camera tool by kilburn, but rather than for an NPC, for the actual player - and permanently. I sort-of know the various commands for getting third person and such, but I don’t seem to be able to find anything that would help me get the attachment of the player. I guess ‘entity:lookupattachment’ is what I am thinking of, but does an active player count as an entity?

I’m sure there is a really simple slution that I have completely glossed over, but for the moment I’m stumped…!

Any illumination on the subject would be greatly appreciated.


Look through Falco’s Spectate script for how he views through other players eyes

local id = pl:LookupAttachment( “eyes” );
local at = pl:GetAttachment( id );

local pos = at.Pos;
local ang = at.Ang;

That’s pretty much how you get the attachment. You can use the CalcView hook to move the view.

It will most likely be flickery due to lack of prediction.

Ahhh! It’s really that simple? I was on the right track at least…

Thanks very much for that!

Yeah I wasn’t expecting much from it, it was more of a ‘I wonder if I can do this…’ type of experiment.

Right, I threw together a very simple script, but a few weird things are happening. I have a few ideas why these happenings might be thje case, but I was wondering what you guys think.

Basically what happens is when I look down, the camera rotates down but further than it should and ends up underground. If its position and rotation is the same as 'eyes, this shouldn’t be the case, surely?

Here is the code:

if( CLIENT ) then

 local function CalcView( pl, origin, angles, fov )
local eyes = pl:GetAttachment( pl:LookupAttachment( "eyes" ) );
    	local view = {
			origin = eyes.Pos,
			angles = eyes.Ang,
			fov = 90, 

	return view;

hook.Add( "CalcView", "ViewMod", CalcView );

end [/lua]

I always hate being the one to ruin everyone’s fun, but I’m known for taking the easy way out of things.

function SpectatePlayer( ply, otherply )
ply:Spectate( OBS_MODE_IN_EYE )
ply:SpectateEntity( otherply )


Yes, it is that easy.


Change your avatar and post more, we need more mature people who want to learn/code Lua in Facepunch.


Edit edit

The OBS_MODE_IN_EYE is static, meaning you cannot use the mouse to change the view.

Hmm. It sounds good, but I don’t think it’s quite what I was looking for. If you’ve used the NPC Camera Tool then it makes putting this into words easier, but what I hope to end up with is to eliminate the view model, make the player’s world model visible and have the camera follow the ‘eyes’ bone rather than just being a detached floating entity that the world model follows about.

I’ll admit it does sound rather pointless, but hey, it’s all for my curiosity. Besides, I find camera views like that more immersive.

Rather than spoon-feeding me the code I need, are there any existing things out there which do similar things to what Im doing? I took a look at cringerpants’ third person script but it’s a bit beyond me T.T

Nonetheless I’ll try anything, it helps expand my understanding of this!

Mature? Me? Hahahaha…heh. I’ll try my best :wink:

Did you look at the npc camera tool? You could try doing it their way - parent a prop to the player’s head bone, then setting the player’s view entity to that prop.

…Why did I not think of that?! I was looking at a different script and I had that one all along >.> Thanks!