Thirdperson on right click. (SWEP help)

I’m going to put it out there, I haven’t been doing LUA in a year or so but I know the basics.

Basically, I’m trying to make it so where the user of the SWEP right clicks with the weapon and it goes third person and I have this here



function SWEP:CalcView( ply, pos, angle, fov )
	if ( !ply:IsValid() or !ply:Alive() or ply:InVehicle() or ply:GetViewEntity() != ply then return end

	local trace = util.TraceLine( {
		start = pos,
		endpos = pos - angle:Forward() * 124,
		filter = { ply:GetActiveWeapon(), ply }
	} )

	if ( trace.Hit ) then pos = trace.HitPos + angle:Forward() * 24 elseif 
	!self.Owner:Crouching() then pos = pos - angle:Forward() * 110 + angle:Up()*8
	elseif self.Owner:Crouching() then pos = pos - angle:Forward() * 110 + angle:Up()*25
	end

	return pos, angle, fov
end


That’s what I’ll be using for the thirdperson function but I don’t know how to tie it into the SecondaryAttack function. Could anyone help me with this?

If you only want it to happen with 1 specific weapon, or just sweps… then adding it to SecondartAttack seems alright. If you want it to happen with all weapons, then we’d want to look at PlayerBindPress for +attack2.


//
// Secondary Attack function; coded to toggle thirdperson...
//
function SWEP:SecondaryAttack( )
	// Get the owner of the weapon
	local _p = self.Owner;

	// If the self.Owner value is bad, and the client is executing the code, and the localplayer is valid, then use it instead...
	if ( !IsValid( _p ) && CLIENT && IsValid( LocalPlayer( ) ) ) then
		_p = LocalPlayer( );
	end

	// We don't need the server to switch to first-person, so we'll just set up client code here...
	if ( CLIENT ) then
		// The way CLIENTs work with SWEPs is they SPAM commands until the SERVER and CLIENT are "synced". It can run the command once, or 10 times.... By using this, it only runs once...
		if ( !IsFirstTimePredicted( ) ) then return; end

		// If player.__Thirdperson is set then it sets it to false. If it is NOT set, or false, it sets it to true
		_p.__ThirdPerson = !_p.__ThirdPerson;
	end
end

Now, for your CalcView… Don’t use ShouldDrawLocalPlayer either by the way… use view.drawviewer = true to avoid needing an extra call…

… Noticed it is SWEP… I’ve never used SWEP CalcView before, but it is highly limiting, so we’ll use a GM CalcView hook override where we only return a value when we need to ( ie thirdperson )


hook.Add( "CalcView", "WeaponCalcViewOverride", function( ply, origin, angles, fov, znear, zfar )
	// Bad checks.. Use IsValid( obj ) instead. If ply isn't valid, it'll report Error IsValid is nil...
	if ( !IsValid( ply ) || !ply:Alive( ) || ply:InVehicle( ) || ply:GetViewEntity( ) != ply then return; end

	// Defind a local var to define whether or not thirdperson is active...
	local _bThirdPerson = ply.__ThirdPerson;

	// If thirdperson isn't, no point in continuing..
	if ( !_bThirdPerson ) then return; end

	// Declare our view table
	local view = { };
	view.origin 			= pos;
	view.angles			= angles;
	view.fov 			= fov;
	view.drawviewer		= _bThirdPerson; -- We used _p above, you're using ply here.. Same thing... But, just setting it to the var is an easy way to ensure it takes over whenever...

	local trace = util.TraceLine( {
		start = pos;
		endpos = pos - angles:Forward( ) * 124;
		filter = {
			ply:GetActiveWeapon( );
			ply;
		};
	} );

	// Define some helpers...
	local _up = angles:Up( );
	local _forward = angles:Forward( );
	-- local _right = angles:Right( ); -- not used
	
	// Issues here.. Not really, but why use ply if you're not going to use it?
	// Also, cover the most unlikely first..... If not crouching covers a LOT... crouching not so much, and hitting a wall is most important...
	if ( trace.Hit ) then
		view.origin = trace.HitPos + _forward * 24;
	elseif ( ply:Crouching( ) then
		view.origin = view.origin - _forward * 110 + _up * 25;
	elseif ( ply:Crouching( ) ) then
		view.origin = view.origin - _forward * 110 + _up * 8;
	end

	// Odd..."view.origin, view.angles, view.fov = func( Weapon, ply, origin*1, angles*1, fov ) -- Note: *1 to copy the object so the child function can't edit it."... Crap, why does this one have to be nerged? If you use the standard CalcView you can achieve the same stuff with the drawviewer leaving ShouldDrawLocalPlayer out of it...
	return view;
end );

Wrote it in this box, so… We’ll see how it turned out…

Another form of ThirdPerson:
https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/over_shoulder_camera_and_crosshair_system/gm_calcview.lua.html

Hopefully these tutorials and information helps. As always, to view the Lua from any of my tutorials to enable copy/pasting ( HTML doesn’t copy/paste well in terms of HTML / CSS Highlighted Lua ), remove .html from the url.
If the thread questions have been solved, please mark the topic as solved by clicking “Mark As Solved” in the upper left-hand corner of the page.

Yeah. I’m just using it for one swep. The code you put there is awesome, thanks for that.

EDIT: Fixed a few errors that I was not thinking on. Thank you.
Thirdperson isn’t working at all and no errors. Interesting.