Alright. I need an effective code to keep my gun and hand objects in the screen when you move and look with the mouse. I tried to use the lock object on command, but it made my models all funky. I posted what I have below. It has some pretty messy math equations I wrote in a few hours

. It works very well when rotating around the Y axis (looking left or right with the mouse), but when I look with the X axis, (looking up or down with the mouse), It looks like it is being rolled right. It works If the camera Y angel is 0, but after that, it just keeps getting worse. Someone help my with my code, or suggest a new one please! Or if you know how to get the lock object on to not skrew up my models, that would be good too. Thanks!
load object "GunFinal.3ds",1
load object "idle2.3ds",2
sync rate 60
sync on
hide mouse
y# = 0
x# = 0
Backdrop on
autocam on
Set camera range 1,5000
Fog on
Fog distance 3000
Fog color RGB(128,128,128)
Color Backdrop RGB(128,128,128)
Make matrix 1,10000,10000,20,20
Load image "grass3.jpg",1
Prepare matrix texture 1,1,1,1
Fill matrix 1,0,1
randomize matrix 1,165
X#=5000.00
Z#=5000.00
yrotate object 1,180
fix object pivot 1
scale object 1,50,50,50
enable object zdepth 1
color object 1, rgb(255,0,0)
scale object 2,100,100,100
load bitmap "hands.bmp",2
get image 3,0,0,1024,512
delete bitmap 2
texture object 2,3
enable object zdepth 2
Do
OldCamAngleY# = CameraAngleY#
OldCamAngleX# = CameraAngleX#
CameraAngleY# = WrapValue(CameraAngleY#+MousemoveX()*0.2)
CameraAngleX# = WrapValue(CameraAngleX#+MousemoveY()*0.2)
If upkey()=1
XTest# = Newxvalue(X#,CameraAngleY#,10)
ZTest# = Newzvalue(Z#,CameraAngleY#,10)
If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
X#=XTest#
Z#=ZTest#
Endif
Endif
If downkey()=1
XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#-180),10)
ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#-180),10)
If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
X#=XTest#
Z#=ZTest#
Endif
Endif
If leftkey()=1
XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#-90),10)
ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#-90),10)
If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
X#=XTest#
Z#=ZTest#
Endif
Endif
If rightkey()=1
XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#+90),10)
ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#+90),10)
If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
X#=XTest#
Z#=ZTest#
Endif
Endif
Yrotate camera CurveAngle(CameraAngleY#,OldCamAngleY#,24)
Xrotate camera CurveAngle(CameraAngleX#,OldCamAngleX#,24)
Y# = Get ground height(1,X#,Z#)
Position Camera X#,Y#+50.00,Z#
Position Object 1, X# + (COS(90.00-CameraAngleY#))*(COS(360-CameraAngleX#)*5),(Y#+50.00)+((SIN(360-CameraAngleX#))*5),(Z#+5.00) - (5.00-(SIN(90.00-CameraAngleY#))*(COS(360-CameraAngleX#)*5))
Position Object 2, X# + (COS(90.00-CameraAngleY#))*(COS(360-CameraAngleX#)*5),(Y#+50.00)+((SIN(360-CameraAngleX#))*5),(Z#+5.00) - (5.00-(SIN(90.00-CameraAngleY#))*(COS(360-CameraAngleX#)*5))
Yrotate object 1, CameraAngleY#
Yrotate object 2, CameraAngleY#
Xrotate object 1, CameraAngleX#
Xrotate object 2, CameraAngleX#
sync
loop
delete object 1
delete object 2
end