Top-Down rotation

Made a thread as I was clogging up the help thread.

This is what I currently have:

[lua]
function MyCalcView(ply, pos, angles, fov)
local view = {}
view.origin = pos+(angles:Up()*300)
view.angles = Angle(90,0,0)
view.fov = fov
return view
end
hook.Add(“CalcView”, “MyCalcView”, MyCalcView)
[/lua]

and

[lua]
function GM:CreateMove(cmd)
if LocalPlayer():Alive() then
gui.EnableScreenClicker(true)
local mx,my = gui.MousePos()
local pos = LocalPlayer():GetShootPos():ToScreen()

    local ang = Angle(0,math.deg(math.atan2(my-pos.y, mx-pos.x)) + 90,0)
    cmd:SetViewAngles(ang)
end

end
[/lua]

I’m calling this client side, it works however if I move my mouse to the left or right of my screen, the player looks the opposite way, if I move my mouse to the top or bottom of my screen he faces the correct way (facing the cursor).

Another problem is that I don’t seem to be able to shoot with gui.EnableScreenClicker(true) set to true, however I need it to be true so my player faces the correct direction? (The direction of the mouse cursor). The player’s camera should never rotate (only the player himself).


function GM:CreateMove( cmd )
	local ang, v, w, x, y
	
	ang = cmd:GetViewAngles( )
	
	x, y = gui.MousePos( )
	
	v, w = ScrW( ) * .5, ScrH( ) * .5

	--This makes the player aim ( relatively ) where the mouse is on the screen.
	--I guess higher resolutions could make this more precise ( for the same reason Fahrenheit is more precise than Celsius ), but whatever.
	--I think I juggled a few ways of doing this until I decided, screw it I'll just atan2. It works better than the other ways
	
	ang.y = math.deg( math.atan2( v - x, w - y ) )

	cmd:SetViewAngles( ang )
end

That appears to move my camera up and down when I move my mouse, it doesn’t rotate the player at all.

That’s the way I accomplished it with the video.

Also:


function GM:GUIMousePressed( mc )
	if mc == MOUSE_LEFT then
		RunConsoleCommand( "+attack" )
	elseif mc == MOUSE_RIGHT then
		RunConsoleCommand( "+attack2" )
	end
end
function GM:GUIMouseReleased( mc )
	if mc == MOUSE_LEFT then
		RunConsoleCommand( "-attack" )
	elseif mc == MOUSE_RIGHT then
		RunConsoleCommand( "-attack2" )
	end
end

Aha, Yes thank’s that worked. Forgot to enable gui.EnableScreenClicker(true). The controls seem weird tho as in he walks the way your mouse is facing if you hold W, in traditional top-down shooters he would always walk up with w no matter which way your character was facing. Not much of a problem but yeah.

You could always handle the movement yourself by setting the move velocity based on which buttons are pressed in a shared move hook. SetupMove comes to mind.

Doing this:

[lua]
function mySetupMove(ply, mv)
if (ply:KeyDown(IN_FORWARD)) then
mv:SetVelocity( Vector(200,0,0) )
return
end
if (ply:KeyDown(IN_MOVERIGHT)) then
mv:SetVelocity( Vector(0,-200,0) )
return
end
if (ply:KeyDown(IN_MOVELEFT)) then
mv:SetVelocity( Vector(0,200,0) )
return
end
if (ply:KeyDown(IN_BACK)) then
mv:SetVelocity( Vector(-200,0,0) )
return
end
end
hook.Add(“SetupMove”, “mySetupMove”, mySetupMove)
[/lua]

Works however it still tries to walk normally even after returning? Thinking about it this isn’t the best way because you won’t be able to move diagonal while holding W and A for example.

Here we go:

[lua]function mySetupMove(ply, mv)
if not (( ply:KeyDown( IN_FORWARD ) ) or ( ply:KeyDown( IN_BACK ) ) or ( ply:KeyDown( IN_MOVELEFT ) ) or ( ply:KeyDown( IN_MOVERIGHT ) )) then return end

if (ply:KeyDown(IN_FORWARD)) then
	if (ply:KeyDown(IN_MOVERIGHT)) then
		mv:SetVelocity( Vector(100,-100,0) )
		return
	elseif (ply:KeyDown(IN_MOVELEFT)) then
		mv:SetVelocity( Vector(100,100,0) )
		return
	else
		mv:SetVelocity( Vector(200,0,0) )
		return
	end
end
if (ply:KeyDown(IN_BACK)) then
	if (ply:KeyDown(IN_MOVERIGHT)) then
		mv:SetVelocity( Vector(-100,-100,0) )
		return
	elseif(ply:KeyDown(IN_MOVELEFT)) then
		mv:SetVelocity( Vector(-100,100,0) )
		return
	else
		mv:SetVelocity( Vector(-200,0,0) )
		return
	end
end
if (ply:KeyDown(IN_MOVERIGHT)) then
    mv:SetVelocity( Vector(0,-200,0) )
    return
end
if (ply:KeyDown(IN_MOVELEFT)) then
    mv:SetVelocity( Vector(0,200,0) )
    return
end

end
hook.Add(“SetupMove”, “mySetupMove”, mySetupMove)[/lua]

Should only run when you’re pressing a movement key, and it should let you move diagonally.

Thanks, that worked however it still tries to walk in the normal direction even after returning after each one.