• Walk towards direction based on keypress
    10 replies, posted
So, I've got my third person stuff working the way I want it to - the player is always aiming towards the crosshair. It's all fun and games. I would like to make it so pressing "W" makes the player walk perpendicular to the camera and not towards where the player is aiming, though. [t]https://dl.dropboxusercontent.com/u/965202/ShareX/2014-06/Garry%27s_Mod_2014-06-05_03-17-15.jpg[/t] For instance, because I am close to the wall and my crosshair is on the wall, pushing W would make him walk alongside the wall. It's a weird inconsistency I'd like to get rid of! [quote]2:58 AM - _Kilburn: you'd have to tamper with the forward speed and side speed in CreateMove 2:58 AM - _Kilburn: but that requires a little bit of trigonometry[/quote] Is this the case or is there anything else I can do to force the player in the right direction?
Pretty much the case if you force the player to look in certain direction. Should be simple enough though.
Alright then, sounds like a plan. I'll get going with it tomorrow and try my best. The trigonometry part of my brain isn't too great but that's what Calculus I brothers are for.
You will have to use: [url]http://wiki.garrysmod.com/page/CUserCmd/SetSideMove[/url] [url]http://wiki.garrysmod.com/page/CUserCmd/SetForwardMove[/url] You can also try messing around with [url]http://wiki.garrysmod.com/page/CUserCmd/SetViewAngles[/url] It might or might not affect view angles. It affects move angles, not view angles.
I found out an easier way I could do it with SetMoveAngles( )! [code]function GM:SetupMove( ply, mv, cmd ) mv:SetMoveAngles( Angle( 0, 0, 0 ) ) end[/code] This is cool and all, but it only works Serverside, and the client's camera is completely clientside with clientside variables. Would it be best to network the camera angle over to the server and if so, what would be the best way to do it? When it comes to networking people say to use datatables and all kinds of other different methods so I'm not sure which method is best for something like this.
In Steam Chat: [lua]if ( ply:KeyDown( bit.bor( IN_FORWARD, IN_BACK, IN_MOVELEFT, IN_MOVERIGHT, IN_ATTACK2 ) ) ) then ply:SetFlag( "view_angle_old", Angle( angles.p, _angle.y, 0 ), true ); ply:SetEyeAngles( Angle( angles.p, _angle.y, 0 ) ); else local _old = ply:GetFlag( "view_angle_old", Angle( 0, 0, 0 ), true ); ply:SetEyeAngles( Angle( angles.p, _old.y, 0 ) ); end[/lua] It works on clientside; it should be clientside with the camera and for proper prediction... Use SetEyeAngles...
You keep talking about "SetFlag" but I have no idea what that is.
I said several times that it is a custom networking function I use. I said since the third value is true, it is no different than using LocalPlayer().view_angle and LocalPlayer().view_angle_old When I Set, it sets the Angle, when I Get, it gets the angle or a default angle to prevent nil. So, LocalPlayer( ).view_angle || Angle( 0, 0, 0 ); would be the working version. [lua]if ( ply:KeyDown( bit.bor( IN_FORWARD, IN_BACK, IN_MOVELEFT, IN_MOVERIGHT, IN_ATTACK2 ) ) ) then LocalPlayer( ).view_angle_old = Angle( angles.p, _angle.y, 0 ); ply:SetEyeAngles( Angle( angles.p, _angle.y, 0 ) ); else local _old = LocalPlayer( ).view_angle_old || Angle( 0, 0, 0 ); ply:SetEyeAngles( Angle( angles.p, _old.y, 0 ) ); end[/lua]
I believe that would disrupt my thirdperson view script. [code]if CLIENT then function thirdPerson( ply, pos, ang, fov, znear, zfar ) if !ply.camera_ang then return ply, pos, ang, fov, znear, zfar end local camPos = pos - ( ply.camera_ang:Forward( ) * 100 ) + ( ply.camera_ang:Right( ) * 45 ) - ( ply.camera_ang:Up( ) * 5 ) local collisionTr = util.TraceLine( { start = ply:EyePos( ), endpos = camPos, filter = ply, } ) local camPos = collisionTr.HitPos local camTr = util.TraceLine( { start = camPos, endpos = camPos + ( ply.camera_ang:Forward( ) * 9999999 ), filter = ply, } ) local camTrDist = camTr.HitPos:Distance( ply:EyePos( ) ) local camTrCH = ( camTr.HitPos - ply:EyePos( ) ):Angle( ) ply:SetEyeAngles( camTrCH ) local view = { } view.origin = camPos view.angles = ply.camera_ang view.fov = fov view.drawviewer = true return view end hook.Add( "CalcView", "thirdPerson", thirdPerson ) function GM:CreateMove( cmd ) local ply = LocalPlayer( ) end hook.Add( "InputMouseApply", "thirdPersonDisableNormalAim", function( cmd, x, y, ang ) if IsValid( LocalPlayer( ) ) then local ply = LocalPlayer( ) if !ply.camera_ang then ply.camera_ang = Angle( 0, 0, 0 ) end ply.camera_ang.p = math.Clamp( math.NormalizeAngle( ply.camera_ang.p + y / 50 ), -90, 90 ) ply.camera_ang.y = math.NormalizeAngle( ply.camera_ang.y - x / 50 ) end return true end) end[/code] Setting the eye angles isn't really going to work if I'm already setting the eye angles to follow the crosshair... [editline]5th June 2014[/editline] [QUOTE=Robotboy655;45005640]You can also try messing around with [url]http://wiki.garrysmod.com/page/CUserCmd/SetViewAngles[/url] It might or might not affect view angles. It affects move angles, not view angles.[/QUOTE] That look liked it would have worked awesome and my model was still aiming towards my crosshair. Strangely enough, however, it caused my bullets to be offset as if it weren't being aimed at my crosshair at all. I wish I could just use GM:SetupMove and cmd:SetMoveAngles( ). It's weird because both the old and new wiki state that it's clientside. Boo!
[code] function thirdPersonWalkingFix( ply, mv, cmd ) if ply.camera_ang then return false end local ply = LocalPlayer( ) local expectedYaw = ply.camera_ang.y local currentYaw = ply:EyeAngles( ).y local offsetYaw = expectedYaw - currentYaw local movementYaw = Vector( mv:GetForwardSpeed( ), mv:GetSideSpeed( ), 0 ) local movementLength = movementYaw:Length( ) movementYaw = movementYaw:Angle( ).y movementYaw = ( Angle( 0, offsetYaw + movementYaw, 0 ) ) movementYaw = movementYaw:Forward( ) mv:SetForwardSpeed( movementYaw.x * movementLength ) mv:SetSideSpeed( movementYaw.y * movementLength ) return true end hook.Add( "SetupMove", "thirdPersonWalkingFix", thirdPersonWalkingFix )[/code] Shazam. I can get a yaw-corrected appropriate speeds, but it doesn't seem like anything is happening to my characters movement at all in this hook. In fact, setting SetForwardSpeed and SetSideSpeed isn't doing anything at all. Could it be that these also don't work clientside? [code] function thirdPersonWalkingFix( cmd ) if !LocalPlayer( ) or !LocalPlayer( ).camera_ang then return false end local ply = LocalPlayer( ) local expectedYaw = ply.camera_ang.y local currentYaw = ply:EyeAngles( ).y local offsetYaw = expectedYaw - currentYaw local movementYaw = Vector( cmd:GetForwardMove( ), cmd:GetSideMove( ), 0 ) local movementLength = movementYaw:Length( ) movementYaw = movementYaw:Angle( ).y movementYaw = ( Angle( 0, offsetYaw + movementYaw, 0 ) ) movementYaw = movementYaw:Forward( ) cmd:SetForwardMove( movementYaw.x * movementLength ) cmd:SetSideMove( movementYaw.y * movementLength ) print( cmd:GetForwardMove( ).." "..cmd:GetSideMove( ) ) return true end[/code] This is CreateMove friendly and also doesn't work.
FIXED. All I needed to do was change: [code]movementYaw = ( Angle( 0, offsetYaw + movementYaw, 0 ) )[/code] into: [code]movementYaw = ( Angle( 0, movementYaw - offsetYaw, 0 ) )[/code] i m vary hapy
Sorry, you need to Log In to post a reply to this thread.