How to fix players camera pitch

At the moment I have a block of code that creates a thirdperson effect, but it’s not working the way I want it to. I have tried numerous variations of the code and can’t get it to work at all.

Basically I want the players shoot cursor to be always level, like on a platform game. And I want the camera to be able to move sideways only and not up or down. This is what I have so far.

Server Side (I wasn’t sure if this is the correct way of doing the eye angles)
[lua]function playerEyeAngle(ply)
local targetPos = Vector(0, 0, 100)
local playerPos = ply:GetShootPos()
local angle = (targetPos - playerPos):Angle()

ply:SetEyeAngles(angle)

end

hook.Add(“Think”, “playerEyeAngle”, playerEyeAngle)[/lua]

Client Side
[lua]function playerChase(ply, pos, angles, fov)
local view = {}

view.origin = pos - (angles:Forward() * 100)
view.angles = angles
view.fov = fov

return view

end

hook.Add(“CalcView”, “playerChase”, playerChase)
hook.Add(“ShouldDrawLocalPlayer”, “drawLocalPlayer”, function(ply)
return true
end)[/lua]

I read some older posts and they are similar to the effect I want but they don’t allow the camera to move sideways, the position is always static as though strafing and moving forward and backwards.



function playerEyeAngle(ply)
    local newpitch = 90--change this to correct pitch level
    local plyang = ply:GetAngles()
    local angle = Angle(newpitch,plyang.y,plyang.r)
     
    ply:SetEyeAngles(angle)
end
hook.Add("Think", "playerEyeAngle", playerEyeAngle)


ply:GetAngles() returns a table we can access each item by using .p,.y,.r for pitch yaw roll next we set pitch to always be the same so that it is always level. http://wiki.garrysmod.com/?title=Angle
http://en.wikipedia.org/wiki/File:Flight_dynamics_with_text.png

also you can call seteyeangles clientside so it doesnt need to be serverside.

Okay I’ve stared at this code for 10 minutes now and it looks perfectly okay to me, but in the console I get the error.


Hook 'playerEyeAngle' Failed: [BradScript\gamemode\cl_hud.lua:217] attempt to index local 'ply' (a nil value)

line 217 is


local plyang = ply:GetAngles()

The “Think” hook does not have any arguments passed to it, so “ply” is nil in that instance. If you still wanted to use a Think hook, you could loop through all the players using player.GetAll().

That’s because Think doesn’t take any parameters.
Use LocalPlayer().

[editline]3rd January 2011[/editline]

:ninja:'d

I thought it looked a bit weird, I was wondering where the ply was being passed from.

It works fine, well when I say fine. When the mouse is moved up or down, and across, eg. If the player was to try moving the cursor to the right but moved the mouse up slightly, it will detect this and push the eye angles back down, but this means there is some serious stiffness when moving the camera side ways.

Is there a way to make it so the mouse cannot move on the Y axis at all? Obviously while looking through the character, and to release the mouse for other things such as popup derma panels?

Edit: I added this line to stop the jittering


if((currentAngles.p <= -10) or (currentAngles.p >= 10)) then

But still it would be nice if I could get a solid fix for moving the eye angle up or down.

Carry on with your CalcView stuff. Edit the angles in here instead of in the Think hook, not only will you probably get better results but it will also look cleaner (since you only have one hook)

[lua]
function playerChase(ply, pos, angles, fov)
local view = {}

view.origin = pos - (angles:Forward() * 100)

local pitchChange = 5
view.angles = angles
view.angles.p = view.angles.p + pitchChange
view.fov = fov
 
return view

end

hook.Add(“CalcView”, “playerChase”, playerChase)
hook.Add(“ShouldDrawLocalPlayer”, “drawLocalPlayer”, function()
return true
end)
[/lua]

You’ll have to mess around with different values for pitchChange, since I doubt 5 will cut it :slight_smile:

I see what you’re trying to do Toneo. But that only locks the camera view, the players are still able to look up and down, but the crosshair is now relative to the pitchAngle.