Making a 2D Gamemode, need help with aiming system.

Hey guys, I am making a 2D Gamemode, all the view stuff is done, most of all stuff is done, except the actual most important aspect in the gamemode, the aiming and shooting. I want my aim-system to be like

, ofcourse it doesn’t have to be a fancy crosshair like that. I just want so that my player can rotate and shoot by using the mouse. This is what I got now: I can’t turn around, only shoot up or down, in the position right. It would be very nice if you guys could help me with this, because i’m having a hard time figuring it out. I got this code now:[lua]local ply = LocalPlayer()
local test = gui.ScreenToVector(gui.MousePos())
local Vec1 = test
local Vec2 = ply:GetShootPos()
local Ang = (Vec1 - Vec2):Angle()
ply:SetEyeAngles( Ang )[/lua]

But that didn’t work, I tried setting gui.EnableScreenClicker, but it won’t look/shoot at the mouse position then.

Can you post your camera code so I can see that too? I think I know how to do this.

[editline]8th March 2011[/editline]

Never mind about the camera, play around with this:

local function DrawCrosshair()
local tr = {}
tr.start = LocalPlayer():GetShootPos()
tr.endpos = tr.start + LocalPlayer():GetAimVector()*1000
tr.mask = MASK_ALL
tr = util.TraceLine(tr)
local pos = tr.HitPos:ToScreen()

You multiply by how many units ahead you want it to be.

That didn’t quite work. I don’t see any circles, or crosshairs on my screen at all, for your information, I placed it in cl_init, if that’s alright.

That’s very weird, it worked fine for me. It should point 1000 units ahead of where you’re aiming.

Did you maybe enable gui.EnableScreenClicker, or? I can’t see any crosshair or circle or whatever.

Wait, I do see the circle when I set line 4 to tr.endpos = tr.start + LocalPlayer():GetAimVector()*0, but then the circle just stays in the middle.

Post me your camera code.

All I did was open sandbox and run that.

[editline]8th March 2011[/editline]

Then adjust the length.

[lua]scrollview = -200

function MyCalcView(ply, pos, angles, fov)

local view = {}
view.origin = pos + Vector(scrollview,0,0)
view.angles = Angle(0,0,0)
view.fov = fov

return view

hook.Add(“CalcView”, “MyCalcView”, MyCalcView)

function GM:Think()

scrollview = math.Clamp( scrollview, -500, -60 )

if ( input.IsKeyDown( KEY_T ) ) then

	scrollview = scrollview + 20

elseif ( input.IsKeyDown( KEY_G ) ) then

	scrollview = scrollview - 20



hook.Add(“ShouldDrawLocalPlayer”, “MyHax ShouldDrawLocalPlayer”, function(ply)
return true

hook.Add(“CreateMove”, “LockMyView”, function(ucmd)
ucmd:SetViewAngles(Angle(ucmd:GetViewAngles().p, -90, 0))

This is my camera code, but, I kind of got it working, it’s just that, the player can’t rotate when he looks to the left. I need to rotate too.

The think hook is for scrolling the view closer and further away. CreateMove is there to prevent the player from looking left or right.

You’re preventing the player from looking left, but he can’t rotate to the left? You just answered your own question.

Well, at the first sentence I ment literally to the left, and at the second sentence I ment of the forward position of the player.

Try commenting out that view angles line.

This blocks the player from looking in the arrow’s directions.

Bump, I really need someone to help me with this.

What is it exactly that is not working?

[editline]9th March 2011[/editline]

Any errors?

I want the aiming system to be like in the Deathrun video, that theres a dot where your mouse is. But the examples given didn’t match my question.

This is what I made for my 2D “RecycleIt!” gamemode, you are free to use it:

function GM:CreateMove( cmd )
local Pl = LocalPlayer()
local MousePosition = gui.ScreenToVector(gui.MousePos())
local MousePositionx, MousePositiony = gui.MousePos()
if ( Pl:Alive() and Pl:Team() != TEAM_SPECTATOR ) then

	cmd:SetForwardMove( cmd:GetSideMove() )
	local ang
	local shootingpos = ( Pl:GetShootPos() ):ToScreen()
	local sumx = (MousePositionx - 10) - shootingpos.x
	local sumy = (MousePositiony - 10) - shootingpos.y
	ang = math.Rad2Deg( math.atan2( sumy, sumx ) )

	if MousePosition.y <= 0 then
		cmd:SetViewAngles( Angle( math.NormalizeAngle(ang), -90, 0 ) )
		cmd:SetViewAngles( Angle( math.NormalizeAngle(180 - ang), 90, 0 ) )
		cmd:SetForwardMove( -cmd:GetSideMove() )
	cmd:SetSideMove( 0 )



Well, it didn’t help the aiming, it flipped my player viewangle, but I can’t aim at all right now.

Of course because that was made for my gamemode. Make the pointer appear, I use an invisible derma panel which uses gui.EnableScreenClicker( true ) to show the pointer. This basically gets the position of the pointer and translates it so that it becomes player’s aim pointer as well changing its angles when left/right.

Oh, I understand now, let me try it.

[editline]9th March 2011[/editline]

Oh man, you’re my hero. It works, although, I can’t shoot right now, any way to make the player so he can shoot?

Might help?
function GM:GUIMousePressed(mc)
if mc == MOUSE_LEFT then RunConsoleCommand("+attack")
elseif mc == MOUSE_RIGHT then RunConsoleCommand("+attack2")

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

This will work only if the pointer is NOT on a derma.

I remembered your thread when I found this:

<3 Ha3