Jump to content

FPS Character Controller Code


Shambler
 Share

Recommended Posts

Ok peeps,

 

here is some sample code for an FPS camera...think of it at as a work in progress since there are a couple of things not quite right but it should give people some idea of where to start.

 

Things that work

 

movement back and forwards,mouselook including limiting up/down angle, collision and jumping.

 

Things that aren't great

 

1) Strafe does not work as expected...does not rotate with the controller...I think someone else has this issue.

2) Crouch would not work so I took it out.

3) Camera moves around wildly for the first second until it stabilises...probably an easy fix but I've got to go out! =)

 

At least the code below should give us a foundation with which to get an fps controller up and running with input from everyone for bug fixes.

Link to comment
Share on other sites

Hi Andy,

 

Thanks for this. Is this a project, how do I use it. I tried creating an empty project 'qwerty' and copying the folders, but Project Manager loads another project....

 

The executable does work. The camera spins and then a black screen. sad.png

 

I copied the map and fps script into darkness awaits. Cool smile.png

 

Robin

Programmer , Intel Quad core, NVIDIA GeForce GT 220, Windows 7 Pro, Galaxy Tab 2 ( 7" and 10"), LE2,LE3,3DWS

Link to comment
Share on other sites

Hi Robin,

 

I just zipped up the project folder, if it doesn't work then I'm unsure.png ?

 

Here's a modification to the jump so you you can only jump when one the ground...

 

function Script:Start()
camera=Camera:Create()
camera:SetParent(self.entity)
camera:SetPosition(0,1.5,0)
camera:SetRotation(0,0,0)
self.camerarotation=Vec3(0)
self.rot=0.0
self.move=0.0
self.strafe=0.0
self.jump=0.0
self.crouch=false
self.mouseposition=Vec2(0,0)
self.movement=Vec3(0,0,0)
self.movespeed=5
self.lookhspeed=0.5
self.lookvspeed=0.75
self.movesmoothing=8
self.looksmoothing=5
self.moveforce=Vec3()
self.mouseposition=Vec2()
self.mousespeed=Vec2()
self.mouseinvert=-1

end

--[[
function Script:UpdateMatrix()

end
]]--
--[[
function Script:UpdateWorld()

end
]]--

function Script:UpdatePhysics()
local window = Window:GetCurrent()
local cx=window:GetClientWidth()/2
local cy=window:GetClientHeight()/2
local camerarotation = camera:GetRotation()
local mousepos=window:GetMousePosition()

window:SetMousePosition(cx,cy)


self.mousespeed.x = Math:Curve(mousepos.x-cx,self.mousespeed.x,self.looksmoothing)*Time:GetSpeed()
self.mousespeed.y = Math:Curve(mousepos.y-cy,self.mousespeed.y,self.looksmoothing)*Time:GetSpeed()
camerarotation.x = camerarotation.x+(self.mouseinvert*self.mousespeed.y)*self.lookvspeed
if camerarotation.x>80 then camerarotation.x=80 end
if camerarotation.x<-80 then camerarotation.x=-80 end
camera:SetRotation(camerarotation)
self.rot=self.rot+self.mousespeed.x*self.lookhspeed
if (window:KeyDown(Key.W)) then self.move=self.movespeed*Time:GetSpeed() end
if (window:KeyDown(Key.S)) then self.move=-(self.movespeed/2)*Time:GetSpeed() end
if (window:KeyDown(Key.D)) then self.strafe=-self.movespeed*Time:GetSpeed() end
if (window:KeyDown(Key.A)) then self.strafe=self.movespeed*Time:GetSpeed() end
if (window:KeyHit(Key.Space)) then
if not self.entity:GetAirborne() then
self.jump=10.0
end
end
self.entity:SetInput(self.rot,self.move,self.strafe,self.jump)
self.move=self.move*0.9
self.strafe=self.strafe*0.9
self.jump=0.0

end

--[[
function Script:Collision(entity, position, normal, speed)

end
]]--
--[[
function Script:Draw()

end
]]--
--[[
function Script:DrawEach(camera)

end
]]--
--[[
function Script:Release()

end
]]--
--[[
function Script:Cleanup()

end
]]--

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...