Hi again
I have the below code for an mmo-style third person camera mode. I'd really appreciate if someone could give it a try and tell me what's wrong!
Press F2 to switch to third person. Move the camera in this mode by bumping the edge of the screen with the cursor and scrolling. Try pressing A and D to turn, and then move the camera up and down. You'll see the camera angles mess up when moving the camera up and down. Can someone please explain how I can fix this?
I'd also like the camera angle to remain static while the player is turning but am not sure how to do that either...
Thanks!
Serverside:
[CODE]
--toggle third person view when the user presses F2
function ToggleThirdPerson( ply )
umsg.Start( "ThirdPerson", ply )
umsg.End()
end
hook.Add( "ShowTeam", "ToggleThirdPerson", ToggleThirdPerson )
[/CODE]
Clientside:
[CODE]
--returns true if no GUIs/menus are visible
local function NoMenu()
return (!gui.IsConsoleVisible() and !gui.IsGameUIVisible())
end
--stores the current view which gets offset based on edge detection
local angLookAround = Angle()
--change how the player strafes - make them turn instead when in third person mode
local function strafe_fix(cmd)
--only run this code if they are in 3rd person mode
if LocalPlayer().ThirdPerson then
--do stuff when the player is trying to strafe
if cmd:GetSideMove() != 0 then
local newangle = cmd:GetViewAngles()
debug_text(newangle:Up())
--find out which way the user is strafing
if cmd:GetSideMove() > 0 then
newangle = newangle + Angle(0,-0.5,0)
cmd:SetViewAngles(newangle)
elseif cmd:GetSideMove() < 0 then
newangle = newangle + Angle(0,0.5,0)
cmd:SetViewAngles(newangle)
end
--this disables strafe by making sure sideways movement speed is always 0
cmd:SetSideMove(0)
end
end
end
hook.Add("CreateMove", "strafe_fix", strafe_fix)
--third person mode
local function ThirdPersonUmsg( data )
if( LocalPlayer().ThirdPerson == nil ) then
LocalPlayer().ThirdPerson = true
else
LocalPlayer().ThirdPerson = !LocalPlayer().ThirdPerson
end
gui.EnableScreenClicker(LocalPlayer().ThirdPerson)
if !LocalPlayer().ThirdPerson then angLookAround = Angle() end
end
usermessage.Hook( "ThirdPerson", ThirdPersonUmsg )
--adjust the default zoom distance for third person mode
local dist = 100
function ThirdpersonFreecam()
if !NoMenu() or !LocalPlayer().ThirdPerson then return end
local x, y = input.GetCursorPos()
local percentage = 1
if x <= (ScrW() / 100) * percentage then
angLookAround = angLookAround + Angle(0, 0.6, 0)
elseif x >= ScrW() - (ScrW() / 100) * percentage then
angLookAround = angLookAround + Angle(0, -0.6, 0)
end
if y <= (ScrH() / 100) * percentage then
angLookAround = angLookAround + Angle(0.3, 0, 0)
elseif y >= ScrH() - (ScrH() / 100) * percentage then
angLookAround = angLookAround - Angle(0.3, 0, 0)
end
end
hook.Add( "Think", "ThirdpersonFreecam:Think", ThirdpersonFreecam)
local iMinDistance = 50
local iMaxDistance = 500
local wp = vgui.GetWorldPanel()
wp:SetCursor("crosshair")
wp:MouseCapture(false)
function wp:OnMouseWheeled(delta)
if delta > 0 then
dist = math.Clamp(dist + 5, iMinDistance, iMaxDistance)
elseif delta < 0 then
dist = math.Clamp(dist - 5, iMinDistance, iMaxDistance)
end
end
local iDistanceTemp = 0
local function DrawThirdPerson(objPl, vecView, angView, iFOV)
--Uncomment below line and modify the * 15 to another number if you want more or less "up" offset
--vecView = vecView + LocalPlayer():GetUp() * 15
if LocalPlayer().ThirdPerson then
local vecOffset = LocalPlayer():GetAimVector()
vecOffset:Rotate(angLookAround)
local data = {}
data.start = vecView
data.endpos = vecView - vecOffset * dist
data.filter = LocalPlayer()
local tr = util.TraceLine(data)
iDistanceTemp = math.Approach(iDistanceTemp, -tr.HitPos:Distance(vecView), FrameTime() * 2000)
local tblView = {}
tblView.origin = vecView + vecOffset * (iDistanceTemp + 25)
tblView.angles = (LocalPlayer():EyePos() - tr.HitPos):Angle()
return tblView
end
end
hook.Add("CalcView", "ThirdPerson", DrawThirdPerson)
--third person
local function ThirdPersonSDLP()
if LocalPlayer().ThirdPerson then
return true
end
end
hook.Add( "ShouldDrawLocalPlayer", "ThirdPersonSDLP", ThirdPersonSDLP )
[/CODE]
Not many people will go through a chunk of code and debug it for you.
Here's some help though: Angle( Pitch, Yaw, Roll ), Vector( X, Y, Z )
- Pitch is tilt along the forward/aft axis
- Yaw is rotation ( so when you press A and D, adjust this and normalize the value )
- Roll is tilt along the left/right axis, you may have seen aircraft doing a barrel roll at an airshow, ie flying straight but rotating the wings around 360 degrees.
X can be left/right or forward/backward depending on your perspective
Y can be left/right or forward/backward depending on your perspective
Z is most commonly the vertical axis ( up/down ), some games like Minecraft, this is x or y and y becomes the vertical axis.
For pitch and roll, set them to 0 in CalcView unless you want an effect where as the user moves up/down the camera pitches slightly up.
For movement, use the X and Y axis for left/right, forward/backward. Use the Z axis to go up and down.
Make sure you modify only the exact Vector / Angle you want to modify, and it helps to keep track of the variable so you can smooth from the current value to the prospective value ( using Lerp ).
You can use CreateMove to detect mouse clicks, scroll-wheel, movement keys, etc.
You can omit ShouldDrawLocalPlayer by using view.drawviewer = true/false; in CalcView.
Sorry, you need to Log In to post a reply to this thread.